*** empty log message ***
[gnus] / lisp / gnus.el
1 ;;; gnus.el --- a newsreader for GNU Emacs
2 ;; Copyright (C) 1987,88,89,90,93,94,95,96 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (eval '(run-hooks 'gnus-load-hook))
30
31 (require 'mail-utils)
32 (require 'timezone)
33 (require 'nnheader)
34
35 (eval-when-compile (require 'cl))
36
37 ;; Site dependent variables.  These variables should be defined in
38 ;; paths.el.
39
40 (defvar gnus-default-nntp-server nil
41   "Specify a default NNTP server.
42 This variable should be defined in paths.el, and should never be set
43 by the user.
44 If you want to change servers, you should use `gnus-select-method'.
45 See the documentation to that variable.")
46
47 (defvar gnus-backup-default-subscribed-newsgroups
48   '("news.announce.newusers" "news.groups.questions" "gnu.emacs.gnus")
49   "Default default new newsgroups the first time Gnus is run.
50 Should be set in paths.el, and shouldn't be touched by the user.")
51
52 (defvar gnus-local-domain nil
53   "Local domain name without a host name.
54 The DOMAINNAME environment variable is used instead if it is defined.
55 If the `system-name' function returns the full Internet name, there is
56 no need to set this variable.")
57
58 (defvar gnus-local-organization nil
59   "String with a description of what organization (if any) the user belongs to.
60 The ORGANIZATION environment variable is used instead if it is defined.
61 If this variable contains a function, this function will be called
62 with the current newsgroup name as the argument.  The function should
63 return a string.
64
65 In any case, if the string (either in the variable, in the environment
66 variable, or returned by the function) is a file name, the contents of
67 this file will be used as the organization.")
68
69 (defvar gnus-use-generic-from nil
70   "If nil, the full host name will be the system name prepended to the domain name.
71 If this is a string, the full host name will be this string.
72 If this is non-nil, non-string, the domain name will be used as the
73 full host name.")
74
75 (defvar gnus-use-generic-path nil
76   "If nil, use the NNTP server name in the Path header.
77 If stringp, use this; if non-nil, use no host name (user name only).")
78
79 ;; Customization variables
80
81 ;; Don't touch this variable.
82 (defvar gnus-nntp-service "nntp"
83   "*NNTP service name (\"nntp\" or 119).
84 This is an obsolete variable, which is scarcely used.  If you use an
85 nntp server for your newsgroup and want to change the port number
86 used to 899, you would say something along these lines:
87
88  (setq gnus-select-method '(nntp \"my.nntp.server\" (nntp-port-number 899)))")
89
90 (defvar gnus-nntpserver-file "/etc/nntpserver"
91   "*A file with only the name of the nntp server in it.")
92
93 ;; This function is used to check both the environment variable
94 ;; NNTPSERVER and the /etc/nntpserver file to see whether one can find
95 ;; an nntp server name default.
96 (defun gnus-getenv-nntpserver ()
97   (or (getenv "NNTPSERVER")
98       (and (file-readable-p gnus-nntpserver-file)
99            (save-excursion
100              (set-buffer (get-buffer-create " *gnus nntp*"))
101              (buffer-disable-undo (current-buffer))
102              (insert-file-contents gnus-nntpserver-file)
103              (let ((name (buffer-string)))
104                (prog1
105                    (if (string-match "^[ \t\n]*$" name)
106                        nil
107                      name)
108                  (kill-buffer (current-buffer))))))))
109
110 (defvar gnus-select-method
111   (nconc
112    (list 'nntp (or (condition-case ()
113                        (gnus-getenv-nntpserver)
114                      (error nil))
115                    (if (and gnus-default-nntp-server
116                             (not (string= gnus-default-nntp-server "")))
117                        gnus-default-nntp-server)
118                    (system-name)))
119    (if (or (null gnus-nntp-service)
120            (equal gnus-nntp-service "nntp"))
121        nil
122      (list gnus-nntp-service)))
123   "*Default method for selecting a newsgroup.
124 This variable should be a list, where the first element is how the
125 news is to be fetched, the second is the address.
126
127 For instance, if you want to get your news via NNTP from
128 \"flab.flab.edu\", you could say:
129
130 (setq gnus-select-method '(nntp \"flab.flab.edu\"))
131
132 If you want to use your local spool, say:
133
134 (setq gnus-select-method (list 'nnspool (system-name)))
135
136 If you use this variable, you must set `gnus-nntp-server' to nil.
137
138 There is a lot more to know about select methods and virtual servers -
139 see the manual for details.")
140
141 (defvar gnus-message-archive-method 
142   '(nnfolder "archive" (nnfolder-directory "~/Mail/archive/")
143              (nnfolder-active-file "~/Mail/archive/active")
144              (nnfolder-get-new-mail nil)
145              (nnfolder-inhibit-expiry t))
146   "*Method used for archiving messages you've sent.
147 This should be a mail method.")
148
149 (defvar gnus-refer-article-method nil
150   "*Preferred method for fetching an article by Message-ID.
151 If you are reading news from the local spool (with nnspool), fetching
152 articles by Message-ID is painfully slow.  By setting this method to an
153 nntp method, you might get acceptable results.
154
155 The value of this variable must be a valid select method as discussed
156 in the documentation of `gnus-select-method'.")
157
158 (defvar gnus-secondary-select-methods nil
159   "*A list of secondary methods that will be used for reading news.
160 This is a list where each element is a complete select method (see
161 `gnus-select-method').
162
163 If, for instance, you want to read your mail with the nnml backend,
164 you could set this variable:
165
166 (setq gnus-secondary-select-methods '((nnml \"\")))")
167
168 (defvar gnus-secondary-servers nil
169   "*List of NNTP servers that the user can choose between interactively.
170 To make Gnus query you for a server, you have to give `gnus' a
171 non-numeric prefix - `C-u M-x gnus', in short.")
172
173 (defvar gnus-nntp-server nil
174   "*The name of the host running the NNTP server.
175 This variable is semi-obsolete.  Use the `gnus-select-method'
176 variable instead.")
177
178 (defvar gnus-startup-file "~/.newsrc"
179   "*Your `.newsrc' file.
180 `.newsrc-SERVER' will be used instead if that exists.")
181
182 (defvar gnus-init-file "~/.gnus"
183   "*Your Gnus elisp startup file.
184 If a file with the .el or .elc suffixes exist, it will be read
185 instead.")
186
187 (defvar gnus-group-faq-directory
188   '("/ftp@mirrors.aol.com:/pub/rtfm/usenet/"
189     "/ftp@src.doc.ic.ac.uk:/usenet/news-FAQS/"
190     "/ftp@ftp.seas.gwu.edu:/pub/rtfm/"
191     "/ftp@rtfm.mit.edu:/pub/usenet/news.answers/"
192     "/ftp@ftp.uni-paderborn.de:/pub/FAQ/"
193     "/ftp@ftp.sunet.se:/pub/usenet/"
194     "/ftp@nctuccca.edu.tw:/USENET/FAQ/"
195     "/ftp@hwarang.postech.ac.kr:/pub/usenet/news.answers/"
196     "/ftp@ftp.hk.super.net:/mirror/faqs/")
197   "*Directory where the group FAQs are stored.
198 This will most commonly be on a remote machine, and the file will be
199 fetched by ange-ftp.
200
201 This variable can also be a list of directories.  In that case, the
202 first element in the list will be used by default, and the others will
203 be used as backup sites.
204
205 Note that Gnus uses an aol machine as the default directory.  If this
206 feels fundamentally unclean, just think of it as a way to finally get
207 something of value back from them.
208
209 If the default site is too slow, try one of these:
210
211    North America: mirrors.aol.com                /pub/rtfm/usenet
212                   ftp.seas.gwu.edu               /pub/rtfm
213                   rtfm.mit.edu                   /pub/usenet/news.answers
214    Europe:        ftp.uni-paderborn.de           /pub/FAQ
215                   src.doc.ic.ac.uk               /usenet/news-FAQS
216                   ftp.sunet.se                   /pub/usenet
217    Asia:          nctuccca.edu.tw                /USENET/FAQ
218                   hwarang.postech.ac.kr          /pub/usenet/news.answers
219                   ftp.hk.super.net               /mirror/faqs")
220
221 (defvar gnus-group-archive-directory
222   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list/"
223   "*The address of the (ding) archives.")
224
225 (defvar gnus-group-recent-archive-directory
226   "/ftp@ftp.hpc.uh.edu:/pub/emacs/ding-list-recent/"
227   "*The address of the most recent (ding) articles.")
228
229 (defvar gnus-default-subscribed-newsgroups nil
230   "*This variable lists what newsgroups should be subscribed the first time Gnus is used.
231 It should be a list of strings.
232 If it is `t', Gnus will not do anything special the first time it is
233 started; it'll just use the normal newsgroups subscription methods.")
234
235 (defvar gnus-use-cross-reference t
236   "*Non-nil means that cross referenced articles will be marked as read.
237 If nil, ignore cross references.  If t, mark articles as read in
238 subscribed newsgroups.  If neither t nor nil, mark as read in all
239 newsgroups.")
240
241 (defvar gnus-single-article-buffer t
242   "*If non-nil, display all articles in the same buffer.
243 If nil, each group will get its own article buffer.")
244
245 (defvar gnus-use-dribble-file t
246   "*Non-nil means that Gnus will use a dribble file to store user updates.
247 If Emacs should crash without saving the .newsrc files, complete
248 information can be restored from the dribble file.")
249
250 (defvar gnus-dribble-directory nil
251   "*The directory where dribble files will be saved.
252 If this variable is nil, the directory where the .newsrc files are
253 saved will be used.")
254
255 (defvar gnus-asynchronous nil
256   "*If non-nil, Gnus will supply backends with data needed for async article fetching.")
257
258 (defvar gnus-kill-summary-on-exit t
259   "*If non-nil, kill the summary buffer when you exit from it.
260 If nil, the summary will become a \"*Dead Summary*\" buffer, and
261 it will be killed sometime later.")
262
263 (defvar gnus-large-newsgroup 200
264   "*The number of articles which indicates a large newsgroup.
265 If the number of articles in a newsgroup is greater than this value,
266 confirmation is required for selecting the newsgroup.")
267
268 ;; Suggested by Andrew Eskilsson <pi92ae@lelle.pt.hk-r.se>.
269 (defvar gnus-no-groups-message "No news is horrible news"
270   "*Message displayed by Gnus when no groups are available.")
271
272 (defvar gnus-use-long-file-name (not (memq system-type '(usg-unix-v xenix)))
273   "*Non-nil means that the default name of a file to save articles in is the group name.
274 If it's nil, the directory form of the group name is used instead.
275
276 If this variable is a list, and the list contains the element
277 `not-score', long file names will not be used for score files; if it
278 contains the element `not-save', long file names will not be used for
279 saving; and if it contains the element `not-kill', long file names
280 will not be used for kill files.")
281
282 (defvar gnus-article-save-directory (or (getenv "SAVEDIR") "~/News/")
283   "*Name of the directory articles will be saved in (default \"~/News\").
284 Initialized from the SAVEDIR environment variable.")
285
286 (defvar gnus-kill-files-directory (or (getenv "SAVEDIR") "~/News/")
287   "*Name of the directory where kill files will be stored (default \"~/News\").
288 Initialized from the SAVEDIR environment variable.")
289
290 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
291   "*A function to save articles in your favorite format.
292 The function must be interactively callable (in other words, it must
293 be an Emacs command).
294
295 Gnus provides the following functions:
296
297 * gnus-summary-save-in-rmail (Rmail format)
298 * gnus-summary-save-in-mail (Unix mail format)
299 * gnus-summary-save-in-folder (MH folder)
300 * gnus-summary-save-in-file (article format).
301 * gnus-summary-save-in-vm (use VM's folder format).")
302
303 (defvar gnus-prompt-before-saving 'always
304   "*This variable says how much prompting is to be done when saving articles.
305 If it is nil, no prompting will be done, and the articles will be
306 saved to the default files.  If this variable is `always', each and
307 every article that is saved will be preceded by a prompt, even when
308 saving large batches of articles.  If this variable is neither nil not
309 `always', there the user will be prompted once for a file name for
310 each invocation of the saving commands.")
311
312 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
313   "*A function generating a file name to save articles in Rmail format.
314 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
315
316 (defvar gnus-mail-save-name (function gnus-plain-save-name)
317   "*A function generating a file name to save articles in Unix mail format.
318 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
319
320 (defvar gnus-folder-save-name (function gnus-folder-save-name)
321   "*A function generating a file name to save articles in MH folder.
322 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
323
324 (defvar gnus-file-save-name (function gnus-numeric-save-name)
325   "*A function generating a file name to save articles in article format.
326 The function is called with NEWSGROUP, HEADERS, and optional
327 LAST-FILE.")
328
329 (defvar gnus-split-methods
330   '((gnus-article-archive-name))
331   "*Variable used to suggest where articles are to be saved.
332 For instance, if you would like to save articles related to Gnus in
333 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
334 you could set this variable to something like:
335
336  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
337    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
338
339 This variable is an alist where the where the key is the match and the
340 value is a list of possible files to save in if the match is non-nil.
341
342 If the match is a string, it is used as a regexp match on the
343 article.  If the match is a symbol, that symbol will be funcalled
344 from the buffer of the article to be saved with the newsgroup as the
345 parameter.  If it is a list, it will be evaled in the same buffer.
346
347 If this form or function returns a string, this string will be used as
348 a possible file name; and if it returns a non-nil list, that list will
349 be used as possible file names.")
350
351 (defvar gnus-move-split-methods nil
352   "*Variable used to suggest where articles are to be moved to.
353 It uses the same syntax as the `gnus-split-methods' variable.")
354
355 (defvar gnus-save-score nil
356   "*If non-nil, save group scoring info.")
357
358 (defvar gnus-use-adaptive-scoring nil
359   "*If non-nil, use some adaptive scoring scheme.")
360
361 (defvar gnus-use-cache nil
362   "*If nil, Gnus will ignore the article cache.
363 If `passive', it will allow entering (and reading) articles
364 explicitly entered into the cache.  If anything else, use the
365 cache to the full extent of the law.")
366
367 (defvar gnus-use-trees nil
368   "*If non-nil, display a thread tree buffer.")
369
370 (defvar gnus-use-grouplens nil
371   "*If non-nil, use GroupLens ratings.")
372
373 (defvar gnus-keep-backlog nil
374   "*If non-nil, Gnus will keep read articles for later re-retrieval.
375 If it is a number N, then Gnus will only keep the last N articles
376 read.  If it is neither nil nor a number, Gnus will keep all read
377 articles.  This is not a good idea.")
378
379 (defvar gnus-use-nocem nil
380   "*If non-nil, Gnus will read NoCeM cancel messages.")
381
382 (defvar gnus-use-demon nil
383   "If non-nil, Gnus might use some demons.")
384
385 (defvar gnus-use-scoring t
386   "*If non-nil, enable scoring.")
387
388 (defvar gnus-use-picons nil
389   "*If non-nil, display picons.")
390
391 (defvar gnus-fetch-old-headers nil
392   "*Non-nil means that Gnus will try to build threads by grabbing old headers.
393 If an unread article in the group refers to an older, already read (or
394 just marked as read) article, the old article will not normally be
395 displayed in the Summary buffer.  If this variable is non-nil, Gnus
396 will attempt to grab the headers to the old articles, and thereby
397 build complete threads.  If it has the value `some', only enough
398 headers to connect otherwise loose threads will be displayed.
399 This variable can also be a number.  In that case, no more than that
400 number of old headers will be fetched.
401
402 The server has to support NOV for any of this to work.")
403
404 ;see gnus-cus.el
405 ;(defvar gnus-visual t
406 ;  "*If non-nil, will do various highlighting.
407 ;If nil, no mouse highlights (or any other highlights) will be
408 ;performed.  This might speed up Gnus some when generating large group
409 ;and summary buffers.")
410
411 (defvar gnus-novice-user t
412   "*Non-nil means that you are a usenet novice.
413 If non-nil, verbose messages may be displayed and confirmations may be
414 required.")
415
416 (defvar gnus-expert-user nil
417   "*Non-nil means that you will never be asked for confirmation about anything.
418 And that means *anything*.")
419
420 (defvar gnus-verbose 7
421   "*Integer that says how verbose Gnus should be.
422 The higher the number, the more messages Gnus will flash to say what
423 it's doing.  At zero, Gnus will be totally mute; at five, Gnus will
424 display most important messages; and at ten, Gnus will keep on
425 jabbering all the time.")
426
427 (defvar gnus-keep-same-level nil
428   "*Non-nil means that the next newsgroup after the current will be on the same level.
429 When you type, for instance, `n' after reading the last article in the
430 current newsgroup, you will go to the next newsgroup.  If this variable
431 is nil, the next newsgroup will be the next from the group
432 buffer.
433 If this variable is non-nil, Gnus will either put you in the
434 next newsgroup with the same level, or, if no such newsgroup is
435 available, the next newsgroup with the lowest possible level higher
436 than the current level.
437 If this variable is `best', Gnus will make the next newsgroup the one
438 with the best level.")
439
440 (defvar gnus-summary-make-false-root 'adopt
441   "*nil means that Gnus won't gather loose threads.
442 If the root of a thread has expired or been read in a previous
443 session, the information necessary to build a complete thread has been
444 lost.  Instead of having many small sub-threads from this original thread
445 scattered all over the summary buffer, Gnus can gather them.
446
447 If non-nil, Gnus will try to gather all loose sub-threads from an
448 original thread into one large thread.
449
450 If this variable is non-nil, it should be one of `none', `adopt',
451 `dummy' or `empty'.
452
453 If this variable is `none', Gnus will not make a false root, but just
454 present the sub-threads after another.
455 If this variable is `dummy', Gnus will create a dummy root that will
456 have all the sub-threads as children.
457 If this variable is `adopt', Gnus will make one of the \"children\"
458 the parent and mark all the step-children as such.
459 If this variable is `empty', the \"children\" are printed with empty
460 subject fields.  (Or rather, they will be printed with a string
461 given by the `gnus-summary-same-subject' variable.)")
462
463 (defvar gnus-summary-gather-exclude-subject "^ *$\\|^(none)$"
464   "*A regexp to match subjects to be excluded from loose thread gathering.
465 As loose thread gathering is done on subjects only, that means that
466 there can be many false gatherings performed.  By rooting out certain
467 common subjects, gathering might become saner.")
468
469 (defvar gnus-summary-gather-subject-limit nil
470   "*Maximum length of subject comparisons when gathering loose threads.
471 Use nil to compare full subjects.  Setting this variable to a low
472 number will help gather threads that have been corrupted by
473 newsreaders chopping off subject lines, but it might also mean that
474 unrelated articles that have subject that happen to begin with the
475 same few characters will be incorrectly gathered.
476
477 If this variable is `fuzzy', Gnus will use a fuzzy algorithm when
478 comparing subjects.")
479
480 (defvar gnus-simplify-ignored-prefixes nil
481   "*Regexp, matches for which are removed from subject lines when simplifying.")
482
483 (defvar gnus-build-sparse-threads nil
484   "*If non-nil, fill in the gaps in threads.
485 If `some', only fill in the gaps that are needed to tie loose threads
486 together.  If `more', fill in all leaf nodes that Gnus can find.  If
487 non-nil and non-`some', fill in all gaps that Gnus manages to guess.")
488
489 (defvar gnus-summary-thread-gathering-function 'gnus-gather-threads-by-subject
490   "Function used for gathering loose threads.
491 There are two pre-defined functions: `gnus-gather-threads-by-subject',
492 which only takes Subjects into consideration; and
493 `gnus-gather-threads-by-references', which compared the References
494 headers of the articles to find matches.")
495
496 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
497 (defvar gnus-summary-same-subject ""
498   "*String indicating that the current article has the same subject as the previous.
499 This variable will only be used if the value of
500 `gnus-summary-make-false-root' is `empty'.")
501
502 (defvar gnus-summary-goto-unread t
503   "*If non-nil, marking commands will go to the next unread article.
504 If `never', \\<gnus-summary-mode-map>\\[gnus-summary-next-page] will go to the next article,
505 whether it is read or not.")
506
507 (defvar gnus-group-goto-unread t
508   "*If non-nil, movement commands will go to the next unread and subscribed group.")
509
510 (defvar gnus-goto-next-group-when-activating t
511   "*If non-nil, the \\<gnus-group-mode-map>\\[gnus-group-get-new-news-this-group] command will advance point to the next group.")
512
513 (defvar gnus-check-new-newsgroups t
514   "*Non-nil means that Gnus will add new newsgroups at startup.
515 If this variable is `ask-server', Gnus will ask the server for new
516 groups since the last time it checked.  This means that the killed list
517 is no longer necessary, so you could set `gnus-save-killed-list' to
518 nil.
519
520 A variant is to have this variable be a list of select methods.  Gnus
521 will then use the `ask-server' method on all these select methods to
522 query for new groups from all those servers.
523
524 Eg.
525   (setq gnus-check-new-newsgroups
526         '((nntp \"some.server\") (nntp \"other.server\")))
527
528 If this variable is nil, then you have to tell Gnus explicitly to
529 check for new newsgroups with \\<gnus-group-mode-map>\\[gnus-find-new-newsgroups].")
530
531 (defvar gnus-check-bogus-newsgroups nil
532   "*Non-nil means that Gnus will check and remove bogus newsgroup at startup.
533 If this variable is nil, then you have to tell Gnus explicitly to
534 check for bogus newsgroups with \\<gnus-group-mode-map>\\[gnus-group-check-bogus-groups].")
535
536 (defvar gnus-read-active-file t
537   "*Non-nil means that Gnus will read the entire active file at startup.
538 If this variable is nil, Gnus will only know about the groups in your
539 `.newsrc' file.
540
541 If this variable is `some', Gnus will try to only read the relevant
542 parts of the active file from the server.  Not all servers support
543 this, and it might be quite slow with other servers, but this should
544 generally be faster than both the t and nil value.
545
546 If you set this variable to nil or `some', you probably still want to
547 be told about new newsgroups that arrive.  To do that, set
548 `gnus-check-new-newsgroups' to `ask-server'.  This may not work
549 properly with all servers.")
550
551 (defvar gnus-level-subscribed 5
552   "*Groups with levels less than or equal to this variable are subscribed.")
553
554 (defvar gnus-level-unsubscribed 7
555   "*Groups with levels less than or equal to this variable are unsubscribed.
556 Groups with levels less than `gnus-level-subscribed', which should be
557 less than this variable, are subscribed.")
558
559 (defvar gnus-level-zombie 8
560   "*Groups with this level are zombie groups.")
561
562 (defvar gnus-level-killed 9
563   "*Groups with this level are killed.")
564
565 (defvar gnus-level-default-subscribed 3
566   "*New subscribed groups will be subscribed at this level.")
567
568 (defvar gnus-level-default-unsubscribed 6
569   "*New unsubscribed groups will be unsubscribed at this level.")
570
571 (defvar gnus-activate-level (1+ gnus-level-subscribed)
572   "*Groups higher than this level won't be activated on startup.
573 Setting this variable to something log might save lots of time when
574 you have many groups that you aren't interested in.")
575
576 (defvar gnus-activate-foreign-newsgroups 4
577   "*If nil, Gnus will not check foreign newsgroups at startup.
578 If it is non-nil, it should be a number between one and nine.  Foreign
579 newsgroups that have a level lower or equal to this number will be
580 activated on startup.  For instance, if you want to active all
581 subscribed newsgroups, but not the rest, you'd set this variable to
582 `gnus-level-subscribed'.
583
584 If you subscribe to lots of newsgroups from different servers, startup
585 might take a while.  By setting this variable to nil, you'll save time,
586 but you won't be told how many unread articles there are in the
587 groups.")
588
589 (defvar gnus-save-newsrc-file t
590   "*Non-nil means that Gnus will save the `.newsrc' file.
591 Gnus always saves its own startup file, which is called
592 \".newsrc.eld\".  The file called \".newsrc\" is in a format that can
593 be readily understood by other newsreaders.  If you don't plan on
594 using other newsreaders, set this variable to nil to save some time on
595 exit.")
596
597 (defvar gnus-save-killed-list t
598   "*If non-nil, save the list of killed groups to the startup file.
599 If you set this variable to nil, you'll save both time (when starting
600 and quitting) and space (both memory and disk), but it will also mean
601 that Gnus has no record of which groups are new and which are old, so
602 the automatic new newsgroups subscription methods become meaningless.
603
604 You should always set `gnus-check-new-newsgroups' to `ask-server' or
605 nil if you set this variable to nil.")
606
607 (defvar gnus-interactive-catchup t
608   "*If non-nil, require your confirmation when catching up a group.")
609
610 (defvar gnus-interactive-post t
611   "*If non-nil, group name will be asked for when posting.")
612
613 (defvar gnus-interactive-exit t
614   "*If non-nil, require your confirmation when exiting Gnus.")
615
616 (defvar gnus-kill-killed t
617   "*If non-nil, Gnus will apply kill files to already killed articles.
618 If it is nil, Gnus will never apply kill files to articles that have
619 already been through the scoring process, which might very well save lots
620 of time.")
621
622 (defvar gnus-extract-address-components 'gnus-extract-address-components
623   "*Function for extracting address components from a From header.
624 Two pre-defined function exist: `gnus-extract-address-components',
625 which is the default, quite fast, and too simplistic solution, and
626 `mail-extract-address-components', which works much better, but is
627 slower.")
628
629 (defvar gnus-summary-default-score 0
630   "*Default article score level.
631 If this variable is nil, scoring will be disabled.")
632
633 (defvar gnus-summary-zcore-fuzz 0
634   "*Fuzziness factor for the zcore in the summary buffer.
635 Articles with scores closer than this to `gnus-summary-default-score'
636 will not be marked.")
637
638 (defvar gnus-simplify-subject-fuzzy-regexp nil
639   "*Strings to be removed when doing fuzzy matches.
640 This can either be a regular expression or list of regular expressions
641 that will be removed from subject strings if fuzzy subject
642 simplification is selected.")
643
644 (defvar gnus-permanently-visible-groups nil
645   "*Regexp to match groups that should always be listed in the group buffer.
646 This means that they will still be listed when there are no unread
647 articles in the groups.")
648
649 (defvar gnus-list-groups-with-ticked-articles t
650   "*If non-nil, list groups that have only ticked articles.
651 If nil, only list groups that have unread articles.")
652
653 (defvar gnus-group-default-list-level gnus-level-subscribed
654   "*Default listing level.
655 Ignored if `gnus-group-use-permanent-levels' is non-nil.")
656
657 (defvar gnus-group-use-permanent-levels nil
658   "*If non-nil, once you set a level, Gnus will use this level.")
659
660 (defvar gnus-group-list-inactive-groups t
661   "*If non-nil, inactive groups will be listed.")
662
663 (defvar gnus-show-mime nil
664   "*If non-nil, do mime processing of articles.
665 The articles will simply be fed to the function given by
666 `gnus-show-mime-method'.")
667
668 (defvar gnus-strict-mime t
669   "*If nil, MIME-decode even if there is no Mime-Version header in the article.")
670
671 (defvar gnus-show-mime-method 'metamail-buffer
672   "*Function to process a MIME message.
673 The function is called from the article buffer.")
674
675 (defvar gnus-decode-encoded-word-method (lambda ())
676   "*Function to decode a MIME encoded-words.
677 The function is called from the article buffer.")
678
679 (defvar gnus-show-threads t
680   "*If non-nil, display threads in summary mode.")
681
682 (defvar gnus-thread-hide-subtree nil
683   "*If non-nil, hide all threads initially.
684 If threads are hidden, you have to run the command
685 `gnus-summary-show-thread' by hand or use `gnus-select-article-hook'
686 to expose hidden threads.")
687
688 (defvar gnus-thread-hide-killed t
689   "*If non-nil, hide killed threads automatically.")
690
691 (defvar gnus-thread-ignore-subject nil
692   "*If non-nil, ignore subjects and do all threading based on the Reference header.
693 If nil, which is the default, articles that have different subjects
694 from their parents will start separate threads.")
695
696 (defvar gnus-thread-operation-ignore-subject t
697   "*If non-nil, subjects will be ignored when doing thread commands.
698 This affects commands like `gnus-summary-kill-thread' and
699 `gnus-summary-lower-thread'.
700
701 If this variable is nil, articles in the same thread with different
702 subjects will not be included in the operation in question.  If this
703 variable is `fuzzy', only articles that have subjects that are fuzzily
704 equal will be included.")
705
706 (defvar gnus-thread-indent-level 4
707   "*Number that says how much each sub-thread should be indented.")
708
709 (defvar gnus-ignored-newsgroups
710   (purecopy (mapconcat 'identity
711                        '("^to\\."       ; not "real" groups
712                          "^[0-9. \t]+ " ; all digits in name
713                          "[][\"#'()]"   ; bogus characters
714                          )
715                        "\\|"))
716   "*A regexp to match uninteresting newsgroups in the active file.
717 Any lines in the active file matching this regular expression are
718 removed from the newsgroup list before anything else is done to it,
719 thus making them effectively non-existent.")
720
721 (defvar gnus-ignored-headers
722   "^Path:\\|^Posting-Version:\\|^Article-I.D.:\\|^Expires:\\|^Date-Received:\\|^References:\\|^Control:\\|^Xref:\\|^Lines:\\|^Posted:\\|^Relay-Version:\\|^Message-ID:\\|^Nf-ID:\\|^Nf-From:\\|^Approved:\\|^Sender:\\|^Received:\\|^Mail-from:"
723   "*All headers that match this regexp will be hidden.
724 This variable can also be a list of regexps of headers to be ignored.
725 If `gnus-visible-headers' is non-nil, this variable will be ignored.")
726
727 (defvar gnus-visible-headers "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-"
728   "*All headers that do not match this regexp will be hidden.
729 This variable can also be a list of regexp of headers to remain visible.
730 If this variable is non-nil, `gnus-ignored-headers' will be ignored.")
731
732 (defvar gnus-sorted-header-list
733   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:"
734     "^Cc:" "^Date:" "^Organization:")
735   "*This variable is a list of regular expressions.
736 If it is non-nil, headers that match the regular expressions will
737 be placed first in the article buffer in the sequence specified by
738 this list.")
739
740 (defvar gnus-boring-article-headers
741   '(empty followup-to reply-to)
742   "*Headers that are only to be displayed if they have interesting data.
743 Possible values in this list are `empty', `newsgroups', `followup-to',
744 `reply-to', and `date'.")
745
746 (defvar gnus-show-all-headers nil
747   "*If non-nil, don't hide any headers.")
748
749 (defvar gnus-save-all-headers t
750   "*If non-nil, don't remove any headers before saving.")
751
752 (defvar gnus-saved-headers gnus-visible-headers
753   "*Headers to keep if `gnus-save-all-headers' is nil.
754 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
755 If that variable is nil, however, all headers that match this regexp
756 will be kept while the rest will be deleted before saving.")
757
758 (defvar gnus-inhibit-startup-message nil
759   "*If non-nil, the startup message will not be displayed.")
760
761 (defvar gnus-signature-separator "^-- *$"
762   "Regexp matching signature separator.")
763
764 (defvar gnus-signature-limit nil
765   "Provide a limit to what is considered a signature.
766 If it is a number, no signature may not be longer (in characters) than
767 that number.  If it is a function, the function will be called without
768 any parameters, and if it returns nil, there is no signature in the
769 buffer.  If it is a string, it will be used as a regexp.  If it
770 matches, the text in question is not a signature.")
771
772 (defvar gnus-auto-extend-newsgroup t
773   "*If non-nil, extend newsgroup forward and backward when requested.")
774
775 (defvar gnus-auto-select-first t
776   "*If nil, don't select the first unread article when entering a group.
777 If this variable is `best', select the highest-scored unread article
778 in the group.  If neither nil nor `best', select the first unread
779 article.
780
781 If you want to prevent automatic selection of the first unread article
782 in some newsgroups, set the variable to nil in
783 `gnus-select-group-hook'.")
784
785 (defvar gnus-auto-select-next t
786   "*If non-nil, offer to go to the next group from the end of the previous.
787 If the value is t and the next newsgroup is empty, Gnus will exit
788 summary mode and go back to group mode.  If the value is neither nil
789 nor t, Gnus will select the following unread newsgroup.  In
790 particular, if the value is the symbol `quietly', the next unread
791 newsgroup will be selected without any confirmation, and if it is
792 `almost-quietly', the next group will be selected without any
793 confirmation if you are located on the last article in the group.
794 Finally, if this variable is `slightly-quietly', the `Z n' command
795 will go to the next group without confirmation.")
796
797 (defvar gnus-auto-select-same nil
798   "*If non-nil, select the next article with the same subject.")
799
800 (defvar gnus-summary-check-current nil
801   "*If non-nil, consider the current article when moving.
802 The \"unread\" movement commands will stay on the same line if the
803 current article is unread.")
804
805 (defvar gnus-auto-center-summary t
806   "*If non-nil, always center the current summary buffer.
807 In particular, if `vertical' do only vertical recentering.  If non-nil
808 and non-`vertical', do both horizontal and vertical recentering.")
809
810 (defvar gnus-break-pages t
811   "*If non-nil, do page breaking on articles.
812 The page delimiter is specified by the `gnus-page-delimiter'
813 variable.")
814
815 (defvar gnus-page-delimiter "^\^L"
816   "*Regexp describing what to use as article page delimiters.
817 The default value is \"^\^L\", which is a form linefeed at the
818 beginning of a line.")
819
820 (defvar gnus-use-full-window t
821   "*If non-nil, use the entire Emacs screen.")
822
823 (defvar gnus-window-configuration nil
824   "Obsolete variable.  See `gnus-buffer-configuration'.")
825
826 (defvar gnus-window-min-width 2
827   "*Minimum width of Gnus buffers.")
828
829 (defvar gnus-window-min-height 1
830   "*Minimum height of Gnus buffers.")
831
832 (defvar gnus-buffer-configuration
833   '((group
834      (vertical 1.0
835                (group 1.0 point)
836                (if gnus-carpal '(group-carpal 4))))
837     (summary
838      (vertical 1.0
839                (summary 1.0 point)
840                (if gnus-carpal '(summary-carpal 4))))
841     (article
842      (cond 
843       (gnus-use-picons
844        '(frame 1.0
845                (vertical 1.0
846                          (summary 0.25 point)
847                          (if gnus-carpal '(summary-carpal 4))
848                          (article 1.0))
849                (vertical ((height . 5) (width . 15)
850                           (user-position . t)
851                           (left . -1) (top . 1))
852                          (picons 1.0))))
853       (gnus-use-trees
854        '(vertical 1.0
855                   (summary 0.25 point)
856                   (tree 0.25)
857                   (article 1.0)))
858       (t
859        '(vertical 1.0
860                  (summary 0.25 point)
861                  (if gnus-carpal '(summary-carpal 4))
862                  (if gnus-use-trees '(tree 0.25))
863                  (article 1.0)))))
864     (server
865      (vertical 1.0
866                (server 1.0 point)
867                (if gnus-carpal '(server-carpal 2))))
868     (browse
869      (vertical 1.0
870                (browse 1.0 point)
871                (if gnus-carpal '(browse-carpal 2))))
872     (group-mail
873      (vertical 1.0
874                (mail 1.0 point)))
875     (summary-mail
876      (vertical 1.0
877                (mail 1.0 point)))
878     (summary-reply
879      (vertical 1.0
880                (article-copy 0.5)
881                (mail 1.0 point)))
882     (pick
883      (vertical 1.0
884                (article 1.0 point)))
885     (info
886      (vertical 1.0
887                (info 1.0 point)))
888     (summary-faq
889      (vertical 1.0
890                (summary 0.25)
891                (faq 1.0 point)))
892     (edit-group
893      (vertical 1.0
894                (group 0.5)
895                (edit-group 1.0 point)))
896     (edit-server
897      (vertical 1.0
898                (server 0.5)
899                (edit-server 1.0 point)))
900     (edit-score
901      (vertical 1.0
902                (summary 0.25)
903                (edit-score 1.0 point)))
904     (post
905      (vertical 1.0
906                (post 1.0 point)))
907     (reply
908      (vertical 1.0
909                (article-copy 0.5)
910                (mail 1.0 point)))
911     (mail-forward
912      (vertical 1.0
913                (mail 1.0 point)))
914     (post-forward
915      (vertical 1.0
916                (post 1.0 point)))
917     (reply-yank
918      (vertical 1.0
919                (mail 1.0 point)))
920     (mail-bounce
921      (vertical 1.0
922                (article 0.5)
923                (mail 1.0 point)))
924     (draft
925      (vertical 1.0
926                (draft 1.0 point)))
927     (pipe
928      (vertical 1.0
929                (summary 0.25 point)
930                (if gnus-carpal '(summary-carpal 4))
931                ("*Shell Command Output*" 1.0)))
932     (followup
933      (vertical 1.0
934                (article-copy 0.5)
935                (post 1.0 point)))
936     (followup-yank
937      (vertical 1.0
938                (post 1.0 point))))
939   "Window configuration for all possible Gnus buffers.
940 This variable is a list of lists.  Each of these lists has a NAME and
941 a RULE.  The NAMEs are commonsense names like `group', which names a
942 rule used when displaying the group buffer; `summary', which names a
943 rule for what happens when you enter a group and do not display an
944 article buffer; and so on.  See the value of this variable for a
945 complete list of NAMEs.
946
947 Each RULE is a list of vectors.  The first element in this vector is
948 the name of the buffer to be displayed; the second element is the
949 percentage of the screen this buffer is to occupy (a number in the
950 0.0-0.99 range); the optional third element is `point', which should
951 be present to denote which buffer point is to go to after making this
952 buffer configuration.")
953
954 (defvar gnus-window-to-buffer
955   '((group . gnus-group-buffer)
956     (summary . gnus-summary-buffer)
957     (article . gnus-article-buffer)
958     (server . gnus-server-buffer)
959     (browse . "*Gnus Browse Server*")
960     (edit-group . gnus-group-edit-buffer)
961     (edit-server . gnus-server-edit-buffer)
962     (group-carpal . gnus-carpal-group-buffer)
963     (summary-carpal . gnus-carpal-summary-buffer)
964     (server-carpal . gnus-carpal-server-buffer)
965     (browse-carpal . gnus-carpal-browse-buffer)
966     (edit-score . gnus-score-edit-buffer)
967     (mail . gnus-mail-buffer)
968     (post . gnus-post-news-buffer)
969     (faq . gnus-faq-buffer)
970     (picons . "*Picons*")
971     (tree . gnus-tree-buffer)
972     (info . gnus-info-buffer)
973     (article-copy . gnus-article-copy)
974     (draft . gnus-draft-buffer))
975   "Mapping from short symbols to buffer names or buffer variables.")
976
977 (defvar gnus-carpal nil
978   "*If non-nil, display clickable icons.")
979
980 (defvar gnus-subscribe-newsgroup-method 'gnus-subscribe-zombies
981   "*Function called with a group name when new group is detected.
982 A few pre-made functions are supplied: `gnus-subscribe-randomly'
983 inserts new groups at the beginning of the list of groups;
984 `gnus-subscribe-alphabetically' inserts new groups in strict
985 alphabetic order; `gnus-subscribe-hierarchically' inserts new groups
986 in hierarchical newsgroup order; `gnus-subscribe-interactively' asks
987 for your decision; `gnus-subscribe-killed' kills all new groups.")
988
989 ;; Suggested by a bug report by Hallvard B Furuseth.
990 ;; <h.b.furuseth@usit.uio.no>.
991 (defvar gnus-subscribe-options-newsgroup-method
992   (function gnus-subscribe-alphabetically)
993   "*This function is called to subscribe newsgroups mentioned on \"options -n\" lines.
994 If, for instance, you want to subscribe to all newsgroups in the
995 \"no\" and \"alt\" hierarchies, you'd put the following in your
996 .newsrc file:
997
998 options -n no.all alt.all
999
1000 Gnus will the subscribe all new newsgroups in these hierarchies with
1001 the subscription method in this variable.")
1002
1003 (defvar gnus-subscribe-hierarchical-interactive nil
1004   "*If non-nil, Gnus will offer to subscribe hierarchically.
1005 When a new hierarchy appears, Gnus will ask the user:
1006
1007 'alt.binaries': Do you want to subscribe to this hierarchy? ([d]ys):
1008
1009 If the user pressed `d', Gnus will descend the hierarchy, `y' will
1010 subscribe to all newsgroups in the hierarchy and `s' will skip this
1011 hierarchy in its entirety.")
1012
1013 (defvar gnus-group-sort-function 'gnus-group-sort-by-alphabet
1014   "*Function used for sorting the group buffer.
1015 This function will be called with group info entries as the arguments
1016 for the groups to be sorted.  Pre-made functions include
1017 `gnus-group-sort-by-alphabet', `gnus-group-sort-by-unread',
1018 `gnus-group-sort-by-level', `gnus-group-sort-by-score', and
1019 `gnus-group-sort-by-rank'.
1020
1021 This variable can also be a list of sorting functions.  In that case,
1022 the most significant sort function should be the last function in the
1023 list.")
1024
1025 ;; Mark variables suggested by Thomas Michanek
1026 ;; <Thomas.Michanek@telelogic.se>.
1027 (defvar gnus-unread-mark ? 
1028   "*Mark used for unread articles.")
1029 (defvar gnus-ticked-mark ?!
1030   "*Mark used for ticked articles.")
1031 (defvar gnus-dormant-mark ??
1032   "*Mark used for dormant articles.")
1033 (defvar gnus-del-mark ?r
1034   "*Mark used for del'd articles.")
1035 (defvar gnus-read-mark ?R
1036   "*Mark used for read articles.")
1037 (defvar gnus-expirable-mark ?E
1038   "*Mark used for expirable articles.")
1039 (defvar gnus-killed-mark ?K
1040   "*Mark used for killed articles.")
1041 (defvar gnus-souped-mark ?F
1042   "*Mark used for killed articles.")
1043 (defvar gnus-kill-file-mark ?X
1044   "*Mark used for articles killed by kill files.")
1045 (defvar gnus-low-score-mark ?Y
1046   "*Mark used for articles with a low score.")
1047 (defvar gnus-catchup-mark ?C
1048   "*Mark used for articles that are caught up.")
1049 (defvar gnus-replied-mark ?A
1050   "*Mark used for articles that have been replied to.")
1051 (defvar gnus-cached-mark ?*
1052   "*Mark used for articles that are in the cache.")
1053 (defvar gnus-saved-mark ?S
1054   "*Mark used for articles that have been saved to.")
1055 (defvar gnus-process-mark ?#
1056   "*Process mark.")
1057 (defvar gnus-ancient-mark ?O
1058   "*Mark used for ancient articles.")
1059 (defvar gnus-sparse-mark ?Q
1060   "*Mark used for sparsely reffed articles.")
1061 (defvar gnus-canceled-mark ?G
1062   "*Mark used for canceled articles.")
1063 (defvar gnus-score-over-mark ?+
1064   "*Score mark used for articles with high scores.")
1065 (defvar gnus-score-below-mark ?-
1066   "*Score mark used for articles with low scores.")
1067 (defvar gnus-empty-thread-mark ? 
1068   "*There is no thread under the article.")
1069 (defvar gnus-not-empty-thread-mark ?=
1070   "*There is a thread under the article.")
1071
1072 (defvar gnus-view-pseudo-asynchronously nil
1073   "*If non-nil, Gnus will view pseudo-articles asynchronously.")
1074
1075 (defvar gnus-view-pseudos nil
1076   "*If `automatic', pseudo-articles will be viewed automatically.
1077 If `not-confirm', pseudos will be viewed automatically, and the user
1078 will not be asked to confirm the command.")
1079
1080 (defvar gnus-view-pseudos-separately t
1081   "*If non-nil, one pseudo-article will be created for each file to be viewed.
1082 If nil, all files that use the same viewing command will be given as a
1083 list of parameters to that command.")
1084
1085 (defvar gnus-insert-pseudo-articles t
1086   "*If non-nil, insert pseudo-articles when decoding articles.")
1087
1088 (defvar gnus-group-line-format "%M%S%p%P%5y: %(%g%)%l\n"
1089   "*Format of group lines.
1090 It works along the same lines as a normal formatting string,
1091 with some simple extensions.
1092
1093 %M    Only marked articles (character, \"*\" or \" \")
1094 %S    Whether the group is subscribed (character, \"U\", \"K\", \"Z\" or \" \")
1095 %L    Level of subscribedness (integer)
1096 %N    Number of unread articles (integer)
1097 %I    Number of dormant articles (integer)
1098 %i    Number of ticked and dormant (integer)
1099 %T    Number of ticked articles (integer)
1100 %R    Number of read articles (integer)
1101 %t    Total number of articles (integer)
1102 %y    Number of unread, unticked articles (integer)
1103 %G    Group name (string)
1104 %g    Qualified group name (string)
1105 %D    Group description (string)
1106 %s    Select method (string)
1107 %o    Moderated group (char, \"m\")
1108 %p    Process mark (char)
1109 %O    Moderated group (string, \"(m)\" or \"\")
1110 %P    Topic indentation (string)
1111 %l    Whether there are GroupLens predictions for this group (string)
1112 %n    Select from where (string)
1113 %z    A string that look like `<%s:%n>' if a foreign select method is used
1114 %u    User defined specifier.  The next character in the format string should
1115       be a letter.  Gnus will call the function gnus-user-format-function-X,
1116       where X is the letter following %u.  The function will be passed the
1117       current header as argument.  The function should return a string, which
1118       will be inserted into the buffer just like information from any other
1119       group specifier.
1120
1121 Text between %( and %) will be highlighted with `gnus-mouse-face' when
1122 the mouse point move inside the area.  There can only be one such area.
1123
1124 Note that this format specification is not always respected.  For
1125 reasons of efficiency, when listing killed groups, this specification
1126 is ignored altogether.  If the spec is changed considerably, your
1127 output may end up looking strange when listing both alive and killed
1128 groups.
1129
1130 If you use %o or %O, reading the active file will be slower and quite
1131 a bit of extra memory will be used. %D will also worsen performance.
1132 Also note that if you change the format specification to include any
1133 of these specs, you must probably re-start Gnus to see them go into
1134 effect.")
1135
1136 (defvar gnus-summary-line-format "%U%R%z%I%(%[%4L: %-20,20n%]%) %s\n"
1137   "*The format specification of the lines in the summary buffer.
1138
1139 It works along the same lines as a normal formatting string,
1140 with some simple extensions.
1141
1142 %N   Article number, left padded with spaces (string)
1143 %S   Subject (string)
1144 %s   Subject if it is at the root of a thread, and \"\" otherwise (string)
1145 %n   Name of the poster (string)
1146 %a   Extracted name of the poster (string)
1147 %A   Extracted address of the poster (string)
1148 %F   Contents of the From: header (string)
1149 %x   Contents of the Xref: header (string)
1150 %D   Date of the article (string)
1151 %d   Date of the article (string) in DD-MMM format
1152 %M   Message-id of the article (string)
1153 %r   References of the article (string)
1154 %c   Number of characters in the article (integer)
1155 %L   Number of lines in the article (integer)
1156 %I   Indentation based on thread level (a string of spaces)
1157 %T   A string with two possible values: 80 spaces if the article
1158      is on thread level two or larger and 0 spaces on level one
1159 %R   \"A\" if this article has been replied to, \" \" otherwise (character)
1160 %U   Status of this article (character, \"R\", \"K\", \"-\" or \" \")
1161 %[   Opening bracket (character, \"[\" or \"<\")
1162 %]   Closing bracket (character, \"]\" or \">\")
1163 %>   Spaces of length thread-level (string)
1164 %<   Spaces of length (- 20 thread-level) (string)
1165 %i   Article score (number)
1166 %z   Article zcore (character)
1167 %t   Number of articles under the current thread (number).
1168 %e   Whether the thread is empty or not (character).
1169 %l   GroupLens score (number)
1170 %u   User defined specifier.  The next character in the format string should
1171      be a letter.  Gnus will call the function gnus-user-format-function-X,
1172      where X is the letter following %u.  The function will be passed the
1173      current header as argument.  The function should return a string, which
1174      will be inserted into the summary just like information from any other
1175      summary specifier.
1176
1177 Text between %( and %) will be highlighted with `gnus-mouse-face'
1178 when the mouse point is placed inside the area.  There can only be one
1179 such area.
1180
1181 The %U (status), %R (replied) and %z (zcore) specs have to be handled
1182 with care.  For reasons of efficiency, Gnus will compute what column
1183 these characters will end up in, and \"hard-code\" that.  This means that
1184 it is illegal to have these specs after a variable-length spec.  Well,
1185 you might not be arrested, but your summary buffer will look strange,
1186 which is bad enough.
1187
1188 The smart choice is to have these specs as for to the left as
1189 possible.
1190
1191 This restriction may disappear in later versions of Gnus.")
1192
1193 (defvar gnus-summary-dummy-line-format
1194   "*  %(:                          :%) %S\n"
1195   "*The format specification for the dummy roots in the summary buffer.
1196 It works along the same lines as a normal formatting string,
1197 with some simple extensions.
1198
1199 %S  The subject")
1200
1201 (defvar gnus-summary-mode-line-format "Gnus: %%b [%A] %Z"
1202   "*The format specification for the summary mode line.
1203 It works along the same lines as a normal formatting string,
1204 with some simple extensions:
1205
1206 %G  Group name
1207 %p  Unprefixed group name
1208 %A  Current article number
1209 %V  Gnus version
1210 %U  Number of unread articles in the group
1211 %e  Number of unselected articles in the group
1212 %Z  A string with unread/unselected article counts
1213 %g  Shortish group name
1214 %S  Subject of the current article
1215 %u  User-defined spec
1216 %s  Current score file name
1217 %d  Number of dormant articles
1218 %r  Number of articles that have been marked as read in this session
1219 %E  Number of articles expunged by the score files")
1220
1221 (defvar gnus-article-mode-line-format "Gnus: %%b %S"
1222   "*The format specification for the article mode line.
1223 See `gnus-summary-mode-line-format' for a closer description.")
1224
1225 (defvar gnus-group-mode-line-format "Gnus: %%b {%M:%S}"
1226   "*The format specification for the group mode line.
1227 It works along the same lines as a normal formatting string,
1228 with some simple extensions:
1229
1230 %S   The native news server.
1231 %M   The native select method.")
1232
1233 (defvar gnus-valid-select-methods
1234   '(("nntp" post address prompt-address)
1235     ("nnspool" post address)
1236     ("nnvirtual" post-mail virtual prompt-address)
1237     ("nnmbox" mail respool address)
1238     ("nnml" mail respool address)
1239     ("nnmh" mail respool address)
1240     ("nndir" post-mail prompt-address address)
1241     ("nneething" none address prompt-address)
1242     ("nndoc" none address prompt-address)
1243     ("nnbabyl" mail address respool)
1244     ("nnkiboze" post address virtual)
1245     ("nnsoup" post-mail address)
1246     ("nndraft" post-mail)
1247     ("nnfolder" mail respool address))
1248   "An alist of valid select methods.
1249 The first element of each list lists should be a string with the name
1250 of the select method.  The other elements may be be the category of
1251 this method (ie. `post', `mail', `none' or whatever) or other
1252 properties that this method has (like being respoolable).
1253 If you implement a new select method, all you should have to change is
1254 this variable.  I think.")
1255
1256 (defvar gnus-updated-mode-lines '(group article summary tree)
1257   "*List of buffers that should update their mode lines.
1258 The list may contain the symbols `group', `article' and `summary'.  If
1259 the corresponding symbol is present, Gnus will keep that mode line
1260 updated with information that may be pertinent.
1261 If this variable is nil, screen refresh may be quicker.")
1262
1263 ;; Added by Keinonen Kari <kk85613@cs.tut.fi>.
1264 (defvar gnus-mode-non-string-length nil
1265   "*Max length of mode-line non-string contents.
1266 If this is nil, Gnus will take space as is needed, leaving the rest
1267 of the modeline intact.")
1268
1269 ;see gnus-cus.el
1270 ;(defvar gnus-mouse-face 'highlight
1271 ;  "*Face used for mouse highlighting in Gnus.
1272 ;No mouse highlights will be done if `gnus-visual' is nil.")
1273
1274 (defvar gnus-summary-mark-below nil
1275   "*Mark all articles with a score below this variable as read.
1276 This variable is local to each summary buffer and usually set by the
1277 score file.")
1278
1279 (defvar gnus-article-sort-functions '(gnus-article-sort-by-number)
1280   "*List of functions used for sorting articles in the summary buffer.
1281 This variable is only used when not using a threaded display.")
1282
1283 (defvar gnus-thread-sort-functions '(gnus-thread-sort-by-number)
1284   "*List of functions used for sorting threads in the summary buffer.
1285 By default, threads are sorted by article number.
1286
1287 Each function takes two threads and return non-nil if the first thread
1288 should be sorted before the other.  If you use more than one function,
1289 the primary sort function should be the last.  You should probably
1290 always include `gnus-thread-sort-by-number' in the list of sorting
1291 functions -- preferably first.
1292
1293 Ready-mady functions include `gnus-thread-sort-by-number',
1294 `gnus-thread-sort-by-author', `gnus-thread-sort-by-subject',
1295 `gnus-thread-sort-by-date', `gnus-thread-sort-by-score' and
1296 `gnus-thread-sort-by-total-score' (see `gnus-thread-score-function').")
1297
1298 (defvar gnus-thread-score-function '+
1299   "*Function used for calculating the total score of a thread.
1300
1301 The function is called with the scores of the article and each
1302 subthread and should then return the score of the thread.
1303
1304 Some functions you can use are `+', `max', or `min'.")
1305
1306 (defvar gnus-summary-expunge-below nil
1307   "All articles that have a score less than this variable will be expunged.")
1308
1309 (defvar gnus-thread-expunge-below nil
1310   "All threads that have a total score less than this variable will be expunged.
1311 See `gnus-thread-score-function' for en explanation of what a
1312 \"thread score\" is.")
1313
1314 (defvar gnus-auto-subscribed-groups
1315   "^nnml\\|^nnfolder\\|^nnmbox\\|^nnmh\\|^nnbabyl"
1316   "*All new groups that match this regexp will be subscribed automatically.
1317 Note that this variable only deals with new groups.  It has no effect
1318 whatsoever on old groups.")
1319
1320 (defvar gnus-options-subscribe nil
1321   "*All new groups matching this regexp will be subscribed unconditionally.
1322 Note that this variable deals only with new newsgroups.  This variable
1323 does not affect old newsgroups.")
1324
1325 (defvar gnus-options-not-subscribe nil
1326   "*All new groups matching this regexp will be ignored.
1327 Note that this variable deals only with new newsgroups.  This variable
1328 does not affect old (already subscribed) newsgroups.")
1329
1330 (defvar gnus-auto-expirable-newsgroups nil
1331   "*Groups in which to automatically mark read articles as expirable.
1332 If non-nil, this should be a regexp that should match all groups in
1333 which to perform auto-expiry.  This only makes sense for mail groups.")
1334
1335 (defvar gnus-total-expirable-newsgroups nil
1336   "*Groups in which to perform expiry of all read articles.
1337 Use with extreme caution.  All groups that match this regexp will be
1338 expiring - which means that all read articles will be deleted after
1339 (say) one week.  (This only goes for mail groups and the like, of
1340 course.)")
1341
1342 (defvar gnus-group-uncollapsed-levels 1
1343   "Number of group name elements to leave alone when making a short group name.")
1344
1345 (defvar gnus-hidden-properties '(invisible t intangible t)
1346   "Property list to use for hiding text.")
1347
1348 (defvar gnus-modtime-botch nil
1349   "*Non-nil means .newsrc should be deleted prior to save.  Its use is
1350 due to the bogus appearance that .newsrc was modified on disc.")
1351
1352 ;; Hooks.
1353
1354 (defvar gnus-group-mode-hook nil
1355   "*A hook for Gnus group mode.")
1356
1357 (defvar gnus-summary-mode-hook nil
1358   "*A hook for Gnus summary mode.
1359 This hook is run before any variables are set in the summary buffer.")
1360
1361 (defvar gnus-article-mode-hook nil
1362   "*A hook for Gnus article mode.")
1363
1364 (defvar gnus-summary-prepare-exit-hook nil
1365   "*A hook called when preparing to exit from the summary buffer.
1366 It calls `gnus-summary-expire-articles' by default.")
1367 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-summary-expire-articles)
1368
1369 (defvar gnus-summary-exit-hook nil
1370   "*A hook called on exit from the summary buffer.")
1371
1372 (defvar gnus-group-catchup-group-hook nil
1373   "*A hook run when catching up a group from the group buffer.")
1374
1375 (defvar gnus-open-server-hook nil
1376   "*A hook called just before opening connection to the news server.")
1377
1378 (defvar gnus-load-hook nil
1379   "*A hook run while Gnus is loaded.")
1380
1381 (defvar gnus-startup-hook nil
1382   "*A hook called at startup.
1383 This hook is called after Gnus is connected to the NNTP server.")
1384
1385 (defvar gnus-get-new-news-hook nil
1386   "*A hook run just before Gnus checks for new news.")
1387
1388 (defvar gnus-after-getting-new-news-hook nil
1389   "*A hook run after Gnus checks for new news.")
1390
1391 (defvar gnus-group-prepare-function 'gnus-group-prepare-flat
1392   "*A function that is called to generate the group buffer.
1393 The function is called with three arguments: The first is a number;
1394 all group with a level less or equal to that number should be listed,
1395 if the second is non-nil, empty groups should also be displayed.  If
1396 the third is non-nil, it is a number.  No groups with a level lower
1397 than this number should be displayed.
1398
1399 The only current function implemented is `gnus-group-prepare-flat'.")
1400
1401 (defvar gnus-group-prepare-hook nil
1402   "*A hook called after the group buffer has been generated.
1403 If you want to modify the group buffer, you can use this hook.")
1404
1405 (defvar gnus-summary-prepare-hook nil
1406   "*A hook called after the summary buffer has been generated.
1407 If you want to modify the summary buffer, you can use this hook.")
1408
1409 (defvar gnus-summary-generate-hook nil
1410   "*A hook run just before generating the summary buffer.
1411 This hook is commonly used to customize threading variables and the
1412 like.")
1413
1414 (defvar gnus-article-prepare-hook nil
1415   "*A hook called after an article has been prepared in the article buffer.
1416 If you want to run a special decoding program like nkf, use this hook.")
1417
1418 ;(defvar gnus-article-display-hook nil
1419 ;  "*A hook called after the article is displayed in the article buffer.
1420 ;The hook is designed to change the contents of the article
1421 ;buffer.  Typical functions that this hook may contain are
1422 ;`gnus-article-hide-headers' (hide selected headers),
1423 ;`gnus-article-maybe-highlight' (perform fancy article highlighting),
1424 ;`gnus-article-hide-signature' (hide signature) and
1425 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
1426 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
1427 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
1428 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
1429
1430 (defvar gnus-article-x-face-command
1431   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
1432   "String or function to be executed to display an X-Face header.
1433 If it is a string, the command will be executed in a sub-shell
1434 asynchronously.  The compressed face will be piped to this command.")
1435
1436 (defvar gnus-article-x-face-too-ugly nil
1437   "Regexp matching posters whose face shouldn't be shown automatically.")
1438
1439 (defvar gnus-select-group-hook nil
1440   "*A hook called when a newsgroup is selected.
1441
1442 If you'd like to simplify subjects like the
1443 `gnus-summary-next-same-subject' command does, you can use the
1444 following hook:
1445
1446  (setq gnus-select-group-hook
1447       (list
1448         (lambda ()
1449           (mapcar (lambda (header)
1450                      (mail-header-set-subject
1451                       header
1452                       (gnus-simplify-subject
1453                        (mail-header-subject header) 're-only)))
1454                   gnus-newsgroup-headers))))")
1455
1456 (defvar gnus-select-article-hook nil
1457   "*A hook called when an article is selected.")
1458
1459 (defvar gnus-apply-kill-hook '(gnus-apply-kill-file)
1460   "*A hook called to apply kill files to a group.
1461 This hook is intended to apply a kill file to the selected newsgroup.
1462 The function `gnus-apply-kill-file' is called by default.
1463
1464 Since a general kill file is too heavy to use only for a few
1465 newsgroups, I recommend you to use a lighter hook function.  For
1466 example, if you'd like to apply a kill file to articles which contains
1467 a string `rmgroup' in subject in newsgroup `control', you can use the
1468 following hook:
1469
1470  (setq gnus-apply-kill-hook
1471       (list
1472         (lambda ()
1473           (cond ((string-match \"control\" gnus-newsgroup-name)
1474                  (gnus-kill \"Subject\" \"rmgroup\")
1475                  (gnus-expunge \"X\"))))))")
1476
1477 (defvar gnus-visual-mark-article-hook
1478   (list 'gnus-highlight-selected-summary)
1479   "*Hook run after selecting an article in the summary buffer.
1480 It is meant to be used for highlighting the article in some way.  It
1481 is not run if `gnus-visual' is nil.")
1482
1483 (defvar gnus-parse-headers-hook nil
1484   "*A hook called before parsing the headers.")
1485
1486 (defvar gnus-exit-group-hook nil
1487   "*A hook called when exiting (not quitting) summary mode.")
1488
1489 (defvar gnus-suspend-gnus-hook nil
1490   "*A hook called when suspending (not exiting) Gnus.")
1491
1492 (defvar gnus-exit-gnus-hook nil
1493   "*A hook called when exiting Gnus.")
1494
1495 (defvar gnus-after-exiting-gnus-hook nil
1496   "*A hook called after exiting Gnus.")
1497
1498 (defvar gnus-save-newsrc-hook nil
1499   "*A hook called before saving any of the newsrc files.")
1500
1501 (defvar gnus-save-quick-newsrc-hook nil
1502   "*A hook called just before saving the quick newsrc file.
1503 Can be used to turn version control on or off.")
1504
1505 (defvar gnus-save-standard-newsrc-hook nil
1506   "*A hook called just before saving the standard newsrc file.
1507 Can be used to turn version control on or off.")
1508
1509 (defvar gnus-summary-update-hook
1510   (list 'gnus-summary-highlight-line)
1511   "*A hook called when a summary line is changed.
1512 The hook will not be called if `gnus-visual' is nil.
1513
1514 The default function `gnus-summary-highlight-line' will
1515 highlight the line according to the `gnus-summary-highlight'
1516 variable.")
1517
1518 (defvar gnus-group-update-hook '(gnus-group-highlight-line)
1519   "*A hook called when a group line is changed.
1520 The hook will not be called if `gnus-visual' is nil.
1521
1522 The default function `gnus-group-highlight-line' will
1523 highlight the line according to the `gnus-group-highlight'
1524 variable.")
1525
1526 (defvar gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read)
1527   "*A hook called when an article is selected for the first time.
1528 The hook is intended to mark an article as read (or unread)
1529 automatically when it is selected.")
1530
1531 (defvar gnus-group-change-level-function nil
1532   "Function run when a group level is changed.
1533 It is called with three parameters -- GROUP, LEVEL and OLDLEVEL.")
1534
1535 ;; Remove any hilit infestation.
1536 (add-hook 'gnus-startup-hook
1537           (lambda ()
1538             (remove-hook 'gnus-summary-prepare-hook
1539                          'hilit-rehighlight-buffer-quietly)
1540             (remove-hook 'gnus-summary-prepare-hook 'hilit-install-line-hooks)
1541             (setq gnus-mark-article-hook '(gnus-summary-mark-read-and-unread-as-read))
1542             (remove-hook 'gnus-article-prepare-hook
1543                          'hilit-rehighlight-buffer-quietly)))
1544
1545 \f
1546 ;; Internal variables
1547
1548 (defvar gnus-server-alist nil
1549   "List of available servers.")
1550
1551 (defvar gnus-topic-indentation "") ;; Obsolete variable.
1552
1553 (defvar gnus-goto-missing-group-function nil)
1554
1555 (defvar gnus-override-subscribe-method nil)
1556
1557 (defvar gnus-group-goto-next-group-function nil
1558   "Function to override finding the next group after listing groups.")
1559
1560 (defconst gnus-article-mark-lists
1561   '((marked . tick) (replied . reply)
1562     (expirable . expire) (killed . killed)
1563     (bookmarks . bookmark) (dormant . dormant)
1564     (scored . score) (saved . save)
1565     (cached . cache)
1566     ))
1567
1568 ;; Avoid highlighting in kill files.
1569 (defvar gnus-summary-inhibit-highlight nil)
1570 (defvar gnus-newsgroup-selected-overlay nil)
1571
1572 (defvar gnus-inhibit-hiding nil)
1573 (defvar gnus-group-indentation "")
1574 (defvar gnus-inhibit-limiting nil)
1575 (defvar gnus-created-frames nil)
1576
1577 (defvar gnus-article-mode-map nil)
1578 (defvar gnus-dribble-buffer nil)
1579 (defvar gnus-headers-retrieved-by nil)
1580 (defvar gnus-article-reply nil)
1581 (defvar gnus-override-method nil)
1582 (defvar gnus-article-check-size nil)
1583
1584 (defvar gnus-current-score-file nil)
1585 (defvar gnus-newsgroup-adaptive-score-file nil)
1586 (defvar gnus-scores-exclude-files nil)
1587
1588 (defvar gnus-opened-servers nil)
1589
1590 (defvar gnus-current-move-group nil)
1591
1592 (defvar gnus-newsgroup-dependencies nil)
1593 (defvar gnus-newsgroup-async nil)
1594 (defconst gnus-group-edit-buffer "*Gnus edit newsgroup*")
1595
1596 (defvar gnus-newsgroup-adaptive nil)
1597
1598 (defvar gnus-summary-display-table nil)
1599 (defvar gnus-summary-display-article-function nil)
1600
1601 (defvar gnus-summary-highlight-line-function nil
1602   "Function called after highlighting a summary line.")
1603
1604 (defvar gnus-group-line-format-alist
1605   `((?M gnus-tmp-marked-mark ?c)
1606     (?S gnus-tmp-subscribed ?c)
1607     (?L gnus-tmp-level ?d)
1608     (?N (cond ((eq number t) "*" )
1609               ((numberp number) 
1610                (int-to-string
1611                 (+ number
1612                    (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1613                    (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))))))
1614               (t number)) ?s)
1615     (?R gnus-tmp-number-of-read ?s)
1616     (?t gnus-tmp-number-total ?d)
1617     (?y gnus-tmp-number-of-unread ?s)
1618     (?I (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked))) ?d)
1619     (?T (gnus-range-length (cdr (assq 'tick gnus-tmp-marked))) ?d)
1620     (?i (+ (gnus-range-length (cdr (assq 'dormant gnus-tmp-marked)))
1621            (gnus-range-length (cdr (assq 'tick gnus-tmp-marked)))) ?d)
1622     (?g gnus-tmp-group ?s)
1623     (?G gnus-tmp-qualified-group ?s)
1624     (?c (gnus-short-group-name gnus-tmp-group) ?s)
1625     (?D gnus-tmp-newsgroup-description ?s)
1626     (?o gnus-tmp-moderated ?c)
1627     (?O gnus-tmp-moderated-string ?s)
1628     (?p gnus-tmp-process-marked ?c)
1629     (?s gnus-tmp-news-server ?s)
1630     (?n gnus-tmp-news-method ?s)
1631     (?P gnus-group-indentation ?s)
1632     (?l gnus-tmp-grouplens ?s)
1633     (?z gnus-tmp-news-method-string ?s)
1634     (?u gnus-tmp-user-defined ?s)))
1635
1636 (defvar gnus-summary-line-format-alist
1637   `((?N ,(macroexpand '(mail-header-number gnus-tmp-header)) ?d)
1638     (?S ,(macroexpand '(mail-header-subject gnus-tmp-header)) ?s)
1639     (?s gnus-tmp-subject-or-nil ?s)
1640     (?n gnus-tmp-name ?s)
1641     (?A (car (cdr (funcall gnus-extract-address-components gnus-tmp-from)))
1642         ?s)
1643     (?a (or (car (funcall gnus-extract-address-components gnus-tmp-from))
1644             gnus-tmp-from) ?s)
1645     (?F gnus-tmp-from ?s)
1646     (?x ,(macroexpand '(mail-header-xref gnus-tmp-header)) ?s)
1647     (?D ,(macroexpand '(mail-header-date gnus-tmp-header)) ?s)
1648     (?d (gnus-dd-mmm (mail-header-date gnus-tmp-header)) ?s)
1649     (?M ,(macroexpand '(mail-header-id gnus-tmp-header)) ?s)
1650     (?r ,(macroexpand '(mail-header-references gnus-tmp-header)) ?s)
1651     (?c (or (mail-header-chars gnus-tmp-header) 0) ?d)
1652     (?L gnus-tmp-lines ?d)
1653     (?I gnus-tmp-indentation ?s)
1654     (?T (if (= gnus-tmp-level 0) "" (make-string (frame-width) ? )) ?s)
1655     (?R gnus-tmp-replied ?c)
1656     (?\[ gnus-tmp-opening-bracket ?c)
1657     (?\] gnus-tmp-closing-bracket ?c)
1658     (?\> (make-string gnus-tmp-level ? ) ?s)
1659     (?\< (make-string (max 0 (- 20 gnus-tmp-level)) ? ) ?s)
1660     (?i gnus-tmp-score ?d)
1661     (?z gnus-tmp-score-char ?c)
1662     (?l (bbb-grouplens-score gnus-tmp-header) ?s)
1663     (?V (gnus-thread-total-score (and (boundp 'thread) (car thread))) ?d)
1664     (?U gnus-tmp-unread ?c)
1665     (?t (gnus-summary-number-of-articles-in-thread
1666          (and (boundp 'thread) (car thread)) gnus-tmp-level)
1667         ?d)
1668     (?e (gnus-summary-number-of-articles-in-thread
1669          (and (boundp 'thread) (car thread)) gnus-tmp-level t)
1670         ?c)
1671     (?u gnus-tmp-user-defined ?s))
1672   "An alist of format specifications that can appear in summary lines,
1673 and what variables they correspond with, along with the type of the
1674 variable (string, integer, character, etc).")
1675
1676 (defvar gnus-summary-dummy-line-format-alist
1677   `((?S gnus-tmp-subject ?s)
1678     (?N gnus-tmp-number ?d)
1679     (?u gnus-tmp-user-defined ?s)))
1680
1681 (defvar gnus-summary-mode-line-format-alist
1682   `((?G gnus-tmp-group-name ?s)
1683     (?g (gnus-short-group-name gnus-tmp-group-name) ?s)
1684     (?p (gnus-group-real-name gnus-tmp-group-name) ?s)
1685     (?A gnus-tmp-article-number ?d)
1686     (?Z gnus-tmp-unread-and-unselected ?s)
1687     (?V gnus-version ?s)
1688     (?U gnus-tmp-unread ?d)
1689     (?S gnus-tmp-subject ?s)
1690     (?e gnus-tmp-unselected ?d)
1691     (?u gnus-tmp-user-defined ?s)
1692     (?d (length gnus-newsgroup-dormant) ?d)
1693     (?t (length gnus-newsgroup-marked) ?d)
1694     (?r (length gnus-newsgroup-reads) ?d)
1695     (?E gnus-newsgroup-expunged-tally ?d)
1696     (?s (gnus-current-score-file-nondirectory) ?s)))
1697
1698 (defvar gnus-article-mode-line-format-alist
1699   gnus-summary-mode-line-format-alist)
1700
1701 (defvar gnus-group-mode-line-format-alist
1702   `((?S gnus-tmp-news-server ?s)
1703     (?M gnus-tmp-news-method ?s)
1704     (?u gnus-tmp-user-defined ?s)))
1705
1706 (defvar gnus-have-read-active-file nil)
1707
1708 (defconst gnus-maintainer
1709   "gnus-bug@ifi.uio.no (The Gnus Bugfixing Girls + Boys)"
1710   "The mail address of the Gnus maintainers.")
1711
1712 (defconst gnus-version "September Gnus v0.49"
1713   "Version number for this version of Gnus.")
1714
1715 (defvar gnus-info-nodes
1716   '((gnus-group-mode            "(gnus)The Group Buffer")
1717     (gnus-summary-mode          "(gnus)The Summary Buffer")
1718     (gnus-article-mode          "(gnus)The Article Buffer"))
1719   "Assoc list of major modes and related Info nodes.")
1720
1721 (defvar gnus-group-buffer "*Group*")
1722 (defvar gnus-summary-buffer "*Summary*")
1723 (defvar gnus-article-buffer "*Article*")
1724 (defvar gnus-server-buffer "*Server*")
1725
1726 (defvar gnus-work-buffer " *gnus work*")
1727
1728 (defvar gnus-original-article-buffer " *Original Article*")
1729 (defvar gnus-original-article nil)
1730
1731 (defvar gnus-buffer-list nil
1732   "Gnus buffers that should be killed on exit.")
1733
1734 (defvar gnus-slave nil
1735   "Whether this Gnus is a slave or not.")
1736
1737 (defvar gnus-variable-list
1738   '(gnus-newsrc-options gnus-newsrc-options-n
1739     gnus-newsrc-last-checked-date
1740     gnus-newsrc-alist gnus-server-alist
1741     gnus-killed-list gnus-zombie-list
1742     gnus-topic-topology gnus-topic-alist
1743     gnus-format-specs)
1744   "Gnus variables saved in the quick startup file.")
1745
1746 (defvar gnus-newsrc-options nil
1747   "Options line in the .newsrc file.")
1748
1749 (defvar gnus-newsrc-options-n nil
1750   "List of regexps representing groups to be subscribed/ignored unconditionally.")
1751
1752 (defvar gnus-newsrc-last-checked-date nil
1753   "Date Gnus last asked server for new newsgroups.")
1754
1755 (defvar gnus-topic-topology nil
1756   "The complete topic hierarchy.")
1757
1758 (defvar gnus-topic-alist nil
1759   "The complete topic-group alist.")
1760
1761 (defvar gnus-newsrc-alist nil
1762   "Assoc list of read articles.
1763 gnus-newsrc-hashtb should be kept so that both hold the same information.")
1764
1765 (defvar gnus-newsrc-hashtb nil
1766   "Hashtable of gnus-newsrc-alist.")
1767
1768 (defvar gnus-killed-list nil
1769   "List of killed newsgroups.")
1770
1771 (defvar gnus-killed-hashtb nil
1772   "Hash table equivalent of gnus-killed-list.")
1773
1774 (defvar gnus-zombie-list nil
1775   "List of almost dead newsgroups.")
1776
1777 (defvar gnus-description-hashtb nil
1778   "Descriptions of newsgroups.")
1779
1780 (defvar gnus-list-of-killed-groups nil
1781   "List of newsgroups that have recently been killed by the user.")
1782
1783 (defvar gnus-active-hashtb nil
1784   "Hashtable of active articles.")
1785
1786 (defvar gnus-moderated-list nil
1787   "List of moderated newsgroups.")
1788
1789 (defvar gnus-group-marked nil)
1790
1791 (defvar gnus-current-startup-file nil
1792   "Startup file for the current host.")
1793
1794 (defvar gnus-last-search-regexp nil
1795   "Default regexp for article search command.")
1796
1797 (defvar gnus-last-shell-command nil
1798   "Default shell command on article.")
1799
1800 (defvar gnus-current-select-method nil
1801   "The current method for selecting a newsgroup.")
1802
1803 (defvar gnus-group-list-mode nil)
1804
1805 (defvar gnus-article-internal-prepare-hook nil)
1806
1807 (defvar gnus-newsgroup-name nil)
1808 (defvar gnus-newsgroup-begin nil)
1809 (defvar gnus-newsgroup-end nil)
1810 (defvar gnus-newsgroup-last-rmail nil)
1811 (defvar gnus-newsgroup-last-mail nil)
1812 (defvar gnus-newsgroup-last-folder nil)
1813 (defvar gnus-newsgroup-last-file nil)
1814 (defvar gnus-newsgroup-auto-expire nil)
1815 (defvar gnus-newsgroup-active nil)
1816
1817 (defvar gnus-newsgroup-data nil)
1818 (defvar gnus-newsgroup-data-reverse nil)
1819 (defvar gnus-newsgroup-limit nil)
1820 (defvar gnus-newsgroup-limits nil)
1821
1822 (defvar gnus-newsgroup-unreads nil
1823   "List of unread articles in the current newsgroup.")
1824
1825 (defvar gnus-newsgroup-unselected nil
1826   "List of unselected unread articles in the current newsgroup.")
1827
1828 (defvar gnus-newsgroup-reads nil
1829   "Alist of read articles and article marks in the current newsgroup.")
1830
1831 (defvar gnus-newsgroup-expunged-tally nil)
1832
1833 (defvar gnus-newsgroup-marked nil
1834   "List of ticked articles in the current newsgroup (a subset of unread art).")
1835
1836 (defvar gnus-newsgroup-killed nil
1837   "List of ranges of articles that have been through the scoring process.")
1838
1839 (defvar gnus-newsgroup-cached nil
1840   "List of articles that come from the article cache.")
1841
1842 (defvar gnus-newsgroup-saved nil
1843   "List of articles that have been saved.")
1844
1845 (defvar gnus-newsgroup-kill-headers nil)
1846
1847 (defvar gnus-newsgroup-replied nil
1848   "List of articles that have been replied to in the current newsgroup.")
1849
1850 (defvar gnus-newsgroup-expirable nil
1851   "List of articles in the current newsgroup that can be expired.")
1852
1853 (defvar gnus-newsgroup-processable nil
1854   "List of articles in the current newsgroup that can be processed.")
1855
1856 (defvar gnus-newsgroup-bookmarks nil
1857   "List of articles in the current newsgroup that have bookmarks.")
1858
1859 (defvar gnus-newsgroup-dormant nil
1860   "List of dormant articles in the current newsgroup.")
1861
1862 (defvar gnus-newsgroup-scored nil
1863   "List of scored articles in the current newsgroup.")
1864
1865 (defvar gnus-newsgroup-headers nil
1866   "List of article headers in the current newsgroup.")
1867
1868 (defvar gnus-newsgroup-threads nil)
1869
1870 (defvar gnus-newsgroup-prepared nil
1871   "Whether the current group has been prepared properly.")
1872
1873 (defvar gnus-newsgroup-ancient nil
1874   "List of `gnus-fetch-old-headers' articles in the current newsgroup.")
1875
1876 (defvar gnus-newsgroup-sparse nil)
1877
1878 (defvar gnus-current-article nil)
1879 (defvar gnus-article-current nil)
1880 (defvar gnus-current-headers nil)
1881 (defvar gnus-have-all-headers nil)
1882 (defvar gnus-last-article nil)
1883 (defvar gnus-newsgroup-history nil)
1884 (defvar gnus-current-kill-article nil)
1885
1886 ;; Save window configuration.
1887 (defvar gnus-prev-winconf nil)
1888
1889 (defvar gnus-summary-mark-positions nil)
1890 (defvar gnus-group-mark-positions nil)
1891
1892 (defvar gnus-reffed-article-number nil)
1893
1894 ;;; Let the byte-compiler know that we know about this variable.
1895 (defvar rmail-default-rmail-file)
1896
1897 (defvar gnus-cache-removable-articles nil)
1898
1899 (defvar gnus-dead-summary nil)
1900
1901 (defconst gnus-summary-local-variables
1902   '(gnus-newsgroup-name
1903     gnus-newsgroup-begin gnus-newsgroup-end
1904     gnus-newsgroup-last-rmail gnus-newsgroup-last-mail
1905     gnus-newsgroup-last-folder gnus-newsgroup-last-file
1906     gnus-newsgroup-auto-expire gnus-newsgroup-unreads
1907     gnus-newsgroup-unselected gnus-newsgroup-marked
1908     gnus-newsgroup-reads gnus-newsgroup-saved
1909     gnus-newsgroup-replied gnus-newsgroup-expirable
1910     gnus-newsgroup-processable gnus-newsgroup-killed
1911     gnus-newsgroup-bookmarks gnus-newsgroup-dormant
1912     gnus-newsgroup-headers gnus-newsgroup-threads
1913     gnus-newsgroup-prepared gnus-summary-highlight-line-function
1914     gnus-current-article gnus-current-headers gnus-have-all-headers
1915     gnus-last-article gnus-article-internal-prepare-hook
1916     gnus-newsgroup-dependencies gnus-newsgroup-selected-overlay
1917     gnus-newsgroup-scored gnus-newsgroup-kill-headers
1918     gnus-newsgroup-async 
1919     gnus-score-alist gnus-current-score-file gnus-summary-expunge-below
1920     gnus-summary-mark-below gnus-newsgroup-active gnus-scores-exclude-files
1921     gnus-newsgroup-history gnus-newsgroup-ancient
1922     gnus-newsgroup-sparse
1923     (gnus-newsgroup-adaptive . gnus-use-adaptive-scoring)
1924     gnus-newsgroup-adaptive-score-file
1925     (gnus-newsgroup-expunged-tally . 0)
1926     gnus-cache-removable-articles gnus-newsgroup-cached
1927     gnus-newsgroup-data gnus-newsgroup-data-reverse
1928     gnus-newsgroup-limit gnus-newsgroup-limits)
1929   "Variables that are buffer-local to the summary buffers.")
1930
1931 (defconst gnus-bug-message
1932   "Sending a bug report to the Gnus Towers.
1933 ========================================
1934
1935 The buffer below is a mail buffer.  When you press `C-c C-c', it will
1936 be sent to the Gnus Bug Exterminators.
1937
1938 At the bottom of the buffer you'll see lots of variable settings.
1939 Please do not delete those.  They will tell the Bug People what your
1940 environment is, so that it will be easier to locate the bugs.
1941
1942 If you have found a bug that makes Emacs go \"beep\", set
1943 debug-on-error to t (`M-x set-variable RET debug-on-error RET t RET')
1944 and include the backtrace in your bug report.
1945
1946 Please describe the bug in annoying, painstaking detail.
1947
1948 Thank you for your help in stamping out bugs.
1949 ")
1950
1951 ;;; End of variables.
1952
1953 ;; Define some autoload functions Gnus might use.
1954 (eval-and-compile
1955
1956   ;; This little mapcar goes through the list below and marks the
1957   ;; symbols in question as autoloaded functions.
1958   (mapcar
1959    (lambda (package)
1960      (let ((interactive (nth 1 (memq ':interactive package))))
1961        (mapcar
1962         (lambda (function)
1963           (let (keymap)
1964             (when (consp function)
1965               (setq keymap (car (memq 'keymap function)))
1966               (setq function (car function)))
1967             (autoload function (car package) nil interactive keymap)))
1968         (if (eq (nth 1 package) ':interactive)
1969             (cdddr package)
1970           (cdr package)))))
1971    '(("metamail" metamail-buffer)
1972      ("info" Info-goto-node)
1973      ("hexl" hexl-hex-string-to-integer)
1974      ("pp" pp pp-to-string pp-eval-expression)
1975      ("mail-extr" mail-extract-address-components)
1976      ("nnmail" nnmail-split-fancy nnmail-article-group)
1977      ("nnvirtual" nnvirtual-catchup-group)
1978      ("timezone" timezone-make-date-arpa-standard timezone-fix-time
1979       timezone-make-sortable-date timezone-make-time-string)
1980      ("sendmail" mail-position-on-field mail-setup)
1981      ("rmailout" rmail-output)
1982      ("rnewspost" news-mail-other-window news-reply-yank-original
1983       news-caesar-buffer-body)
1984      ("rmail" rmail-insert-rmail-file-header rmail-count-new-messages
1985       rmail-show-message)
1986      ("gnus-soup" :interactive t
1987       gnus-group-brew-soup gnus-brew-soup gnus-soup-add-article
1988       gnus-soup-send-replies gnus-soup-save-areas gnus-soup-pack-packet)
1989      ("nnsoup" nnsoup-pack-replies)
1990      ("gnus-mh" gnus-mh-mail-setup gnus-summary-save-article-folder
1991       gnus-Folder-save-name gnus-folder-save-name)
1992      ("gnus-mh" :interactive t gnus-summary-save-in-folder)
1993      ("gnus-vis" gnus-group-make-menu-bar gnus-summary-make-menu-bar
1994       gnus-server-make-menu-bar gnus-article-make-menu-bar
1995       gnus-browse-make-menu-bar gnus-highlight-selected-summary
1996       gnus-summary-highlight-line gnus-carpal-setup-buffer
1997       gnus-group-highlight-line
1998       gnus-article-add-button gnus-insert-next-page-button
1999       gnus-insert-prev-page-button gnus-visual-turn-off-edit-menu)
2000      ("gnus-vis" :interactive t
2001       gnus-article-push-button gnus-article-press-button
2002       gnus-article-highlight gnus-article-highlight-some
2003       gnus-article-highlight-headers gnus-article-highlight-signature
2004       gnus-article-add-buttons gnus-article-add-buttons-to-head
2005       gnus-article-next-button gnus-article-prev-button)
2006      ("gnus-demon" gnus-demon-add-nocem gnus-demon-add-scanmail
2007       gnus-demon-add-disconnection gnus-demon-add-handler
2008       gnus-demon-remove-handler)
2009      ("gnus-demon" :interactive t
2010       gnus-demon-init gnus-demon-cancel)
2011      ("gnus-salt" gnus-highlight-selected-tree gnus-possibly-generate-tree
2012       gnus-tree-open gnus-tree-close)
2013      ("gnus-nocem" gnus-nocem-scan-groups gnus-nocem-close
2014       gnus-nocem-unwanted-article-p)
2015      ("gnus-srvr" gnus-enter-server-buffer gnus-server-set-info)
2016      ("gnus-srvr" gnus-browse-foreign-server)
2017      ("gnus-cite" :interactive t
2018       gnus-article-highlight-citation gnus-article-hide-citation-maybe
2019       gnus-article-hide-citation gnus-article-fill-cited-article)
2020      ("gnus-kill" gnus-kill gnus-apply-kill-file-internal
2021       gnus-kill-file-edit-file gnus-kill-file-raise-followups-to-author
2022       gnus-execute gnus-expunge)
2023      ("gnus-cache" gnus-cache-possibly-enter-article gnus-cache-save-buffers
2024       gnus-cache-possibly-remove-articles gnus-cache-request-article
2025       gnus-cache-retrieve-headers gnus-cache-possibly-alter-active
2026       gnus-cache-enter-remove-article gnus-cached-article-p
2027       gnus-cache-open gnus-cache-close gnus-cache-update-article)
2028      ("gnus-cache" :interactive t gnus-jog-cache gnus-cache-enter-article
2029       gnus-cache-remove-article)
2030      ("gnus-score" :interactive t
2031       gnus-summary-increase-score gnus-summary-lower-score
2032       gnus-score-flush-cache gnus-score-close
2033       gnus-score-raise-same-subject-and-select
2034       gnus-score-raise-same-subject gnus-score-default
2035       gnus-score-raise-thread gnus-score-lower-same-subject-and-select
2036       gnus-score-lower-same-subject gnus-score-lower-thread
2037       gnus-possibly-score-headers)
2038      ("gnus-score"
2039       (gnus-summary-score-map keymap) gnus-score-save gnus-score-headers
2040       gnus-current-score-file-nondirectory gnus-score-adaptive
2041       gnus-score-find-trace gnus-score-file-name)
2042      ("gnus-edit" :interactive t gnus-score-customize)
2043      ("gnus-topic" :interactive t gnus-topic-mode)
2044      ("gnus-topic" gnus-topic-remove-group)
2045      ("gnus-salt" :interactive t gnus-pick-mode gnus-binary-mode)
2046      ("gnus-uu" (gnus-uu-extract-map keymap) (gnus-uu-mark-map keymap))
2047      ("gnus-uu" :interactive t
2048       gnus-uu-digest-mail-forward gnus-uu-digest-post-forward
2049       gnus-uu-mark-series gnus-uu-mark-region gnus-uu-mark-buffer
2050       gnus-uu-mark-by-regexp gnus-uu-mark-all
2051       gnus-uu-mark-sparse gnus-uu-mark-thread gnus-uu-decode-uu
2052       gnus-uu-decode-uu-and-save gnus-uu-decode-unshar
2053       gnus-uu-decode-unshar-and-save gnus-uu-decode-save
2054       gnus-uu-decode-binhex gnus-uu-decode-uu-view
2055       gnus-uu-decode-uu-and-save-view gnus-uu-decode-unshar-view
2056       gnus-uu-decode-unshar-and-save-view gnus-uu-decode-save-view
2057       gnus-uu-decode-binhex-view)
2058      ("gnus-msg" (gnus-summary-send-map keymap)
2059       gnus-mail-yank-original gnus-mail-send-and-exit
2060       gnus-sendmail-setup-mail gnus-article-mail
2061       gnus-inews-message-id gnus-new-mail gnus-mail-reply)
2062      ("gnus-msg" :interactive t
2063       gnus-group-post-news gnus-group-mail gnus-summary-post-news
2064       gnus-summary-followup gnus-summary-followup-with-original
2065       gnus-summary-followup-and-reply
2066       gnus-summary-followup-and-reply-with-original
2067       gnus-summary-cancel-article gnus-summary-supersede-article
2068       gnus-post-news gnus-inews-news gnus-cancel-news
2069       gnus-summary-reply gnus-summary-reply-with-original
2070       gnus-summary-mail-forward gnus-summary-mail-other-window
2071       gnus-bug)
2072      ("gnus-picon" :interactive t gnus-article-display-picons
2073       gnus-group-display-picons gnus-picons-article-display-x-face)
2074      ("gnus-gl" bbb-login bbb-logout bbb-grouplens-group-p 
2075       gnus-grouplens-mode)
2076      ("gnus-vm" gnus-vm-mail-setup)
2077      ("gnus-vm" :interactive t gnus-summary-save-in-vm
2078       gnus-summary-save-article-vm gnus-yank-article))))
2079
2080 \f
2081
2082 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2083 ;; If you want the cursor to go somewhere else, set these two
2084 ;; functions in some startup hook to whatever you want.
2085 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
2086 (defalias 'gnus-group-position-point 'gnus-goto-colon)
2087
2088 ;;; Various macros and substs.
2089
2090 (defun gnus-header-from (header)
2091   (mail-header-from header))
2092
2093 (defmacro gnus-eval-in-buffer-window (buffer &rest forms)
2094   "Pop to BUFFER, evaluate FORMS, and then return to the original window."
2095   `(let ((GnusStartBufferWindow (selected-window)))
2096      (unwind-protect
2097          (progn
2098            (pop-to-buffer ,buffer)
2099            ,@forms)
2100        (select-window GnusStartBufferWindow))))
2101
2102 (defmacro gnus-gethash (string hashtable)
2103   "Get hash value of STRING in HASHTABLE."
2104   `(symbol-value (intern-soft ,string ,hashtable)))
2105
2106 (defmacro gnus-sethash (string value hashtable)
2107   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2108   `(set (intern ,string ,hashtable) ,value))
2109
2110 (defmacro gnus-intern-safe (string hashtable)
2111   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
2112   `(let ((symbol (intern ,string ,hashtable)))
2113      (or (boundp symbol)
2114          (set symbol nil))
2115      symbol))
2116
2117 (defmacro gnus-group-unread (group)
2118   "Get the currently computed number of unread articles in GROUP."
2119   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
2120
2121 (defmacro gnus-group-entry (group)
2122   "Get the newsrc entry for GROUP."
2123   `(gnus-gethash ,group gnus-newsrc-hashtb))
2124
2125 (defmacro gnus-active (group)
2126   "Get active info on GROUP."
2127   `(gnus-gethash ,group gnus-active-hashtb))
2128
2129 (defmacro gnus-set-active (group active)
2130   "Set GROUP's active info."
2131   `(gnus-sethash ,group ,active gnus-active-hashtb))
2132
2133 ;; modified by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2134 ;;   function `substring' might cut on a middle of multi-octet
2135 ;;   character.
2136 (defun gnus-truncate-string (str width)
2137   (substring str 0 width))
2138
2139 ;; Added by Geoffrey T. Dairiki <dairiki@u.washington.edu>.  A safe way
2140 ;; to limit the length of a string.  This function is necessary since
2141 ;; `(substr "abc" 0 30)' pukes with "Args out of range".
2142 (defsubst gnus-limit-string (str width)
2143   (if (> (length str) width)
2144       (substring str 0 width)
2145     str))
2146
2147 (defsubst gnus-simplify-subject-re (subject)
2148   "Remove \"Re:\" from subject lines."
2149   (if (string-match "^[Rr][Ee]: *" subject)
2150       (substring subject (match-end 0))
2151     subject))
2152
2153 (defsubst gnus-goto-char (point)
2154   (and point (goto-char point)))
2155
2156 (defmacro gnus-buffer-exists-p (buffer)
2157   `(and ,buffer
2158         (funcall (if (stringp ,buffer) 'get-buffer 'buffer-name)
2159                  ,buffer)))
2160
2161 (defmacro gnus-kill-buffer (buffer)
2162   `(let ((buf ,buffer))
2163      (if (gnus-buffer-exists-p buf)
2164          (kill-buffer buf))))
2165
2166 (defsubst gnus-point-at-bol ()
2167   "Return point at the beginning of the line."
2168   (let ((p (point)))
2169     (beginning-of-line)
2170     (prog1
2171         (point)
2172       (goto-char p))))
2173
2174 (defsubst gnus-point-at-eol ()
2175   "Return point at the end of the line."
2176   (let ((p (point)))
2177     (end-of-line)
2178     (prog1
2179         (point)
2180       (goto-char p))))
2181
2182 (defun gnus-alive-p ()
2183   "Say whether Gnus is running or not."
2184   (and gnus-group-buffer
2185        (get-buffer gnus-group-buffer)))
2186
2187 ;; Delete the current line (and the next N lines.);
2188 (defmacro gnus-delete-line (&optional n)
2189   `(delete-region (progn (beginning-of-line) (point))
2190                   (progn (forward-line ,(or n 1)) (point))))
2191
2192 ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
2193 (defvar gnus-init-inhibit nil)
2194 (defun gnus-read-init-file (&optional inhibit-next)
2195   (if gnus-init-inhibit
2196       (setq gnus-init-inhibit nil)
2197     (setq gnus-init-inhibit inhibit-next)
2198     (and gnus-init-file
2199          (or (and (file-exists-p gnus-init-file)
2200                   ;; Don't try to load a directory.
2201                   (not (file-directory-p gnus-init-file)))
2202              (file-exists-p (concat gnus-init-file ".el"))
2203              (file-exists-p (concat gnus-init-file ".elc")))
2204          (condition-case var
2205              (load gnus-init-file nil t)
2206            (error
2207             (error "Error in %s: %s" gnus-init-file var))))))
2208
2209 ;; Info access macros.
2210
2211 (defmacro gnus-info-group (info)
2212   `(nth 0 ,info))
2213 (defmacro gnus-info-rank (info)
2214   `(nth 1 ,info))
2215 (defmacro gnus-info-read (info)
2216   `(nth 2 ,info))
2217 (defmacro gnus-info-marks (info)
2218   `(nth 3 ,info))
2219 (defmacro gnus-info-method (info)
2220   `(nth 4 ,info))
2221 (defmacro gnus-info-params (info)
2222   `(nth 5 ,info))
2223
2224 (defmacro gnus-info-level (info)
2225   `(let ((rank (gnus-info-rank ,info)))
2226      (if (consp rank)
2227          (car rank)
2228        rank)))
2229 (defmacro gnus-info-score (info)
2230   `(let ((rank (gnus-info-rank ,info)))
2231      (or (and (consp rank) (cdr rank)) 0)))
2232
2233 (defmacro gnus-info-set-group (info group)
2234   `(setcar ,info ,group))
2235 (defmacro gnus-info-set-rank (info rank)
2236   `(setcar (nthcdr 1 ,info) ,rank))
2237 (defmacro gnus-info-set-read (info read)
2238   `(setcar (nthcdr 2 ,info) ,read))
2239 (defmacro gnus-info-set-marks (info marks)
2240   `(setcar (nthcdr 3 ,info) ,marks))
2241 (defmacro gnus-info-set-method (info method)
2242   `(setcar (nthcdr 4 ,info) ,method))
2243 (defmacro gnus-info-set-params (info params)
2244   `(setcar (nthcdr 5 ,info) ,params))
2245
2246 (defmacro gnus-info-set-level (info level)
2247   `(let ((rank (cdr ,info)))
2248      (if (consp (car rank))
2249          (setcar (car rank) ,level)
2250        (setcar rank ,level))))
2251 (defmacro gnus-info-set-score (info score)
2252   `(let ((rank (cdr ,info)))
2253      (if (consp (car rank))
2254          (setcdr (car rank) ,score)
2255        (setcar rank (cons (car rank) ,score)))))
2256
2257 (defmacro gnus-get-info (group)
2258   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
2259
2260 (defun gnus-byte-code (func)
2261   "Return a form that can be `eval'ed based on FUNC."
2262   (let ((fval (symbol-function func)))
2263     (if (byte-code-function-p fval)
2264         (let ((flist (append fval nil)))
2265           (setcar flist 'byte-code)
2266           flist)
2267       (cons 'progn (cddr fval)))))
2268
2269 ;;; Load the compatability functions.
2270
2271 (require 'gnus-cus)
2272 (require 'gnus-ems)
2273
2274 \f
2275
2276 ;; Format specs.  The chunks below are the machine-generated forms
2277 ;; that are to be evaled as the result of the default format strings.
2278 ;; We write them in here to get them byte-compiled.  That way the
2279 ;; default actions will be quite fast, while still retaining the full
2280 ;; flexibility of the user-defined format specs.
2281
2282 ;; First we have lots of dummy defvars to let the compiler know these
2283 ;; are really dynamic variables.
2284
2285 (defvar gnus-tmp-unread)
2286 (defvar gnus-tmp-replied)
2287 (defvar gnus-tmp-score-char)
2288 (defvar gnus-tmp-indentation)
2289 (defvar gnus-tmp-opening-bracket)
2290 (defvar gnus-tmp-lines)
2291 (defvar gnus-tmp-name)
2292 (defvar gnus-tmp-closing-bracket)
2293 (defvar gnus-tmp-subject-or-nil)
2294 (defvar gnus-tmp-subject)
2295 (defvar gnus-tmp-marked)
2296 (defvar gnus-tmp-marked-mark)
2297 (defvar gnus-tmp-subscribed)
2298 (defvar gnus-tmp-process-marked)
2299 (defvar gnus-tmp-number-of-unread)
2300 (defvar gnus-tmp-group-name)
2301 (defvar gnus-tmp-group)
2302 (defvar gnus-tmp-article-number)
2303 (defvar gnus-tmp-unread-and-unselected)
2304 (defvar gnus-tmp-news-method)
2305 (defvar gnus-tmp-news-server)
2306 (defvar gnus-tmp-article-number)
2307 (defvar gnus-mouse-face)
2308 (defvar gnus-mouse-face-prop)
2309
2310 (defun gnus-summary-line-format-spec ()
2311   (insert gnus-tmp-unread gnus-tmp-replied
2312           gnus-tmp-score-char gnus-tmp-indentation)
2313   (put-text-property
2314    (point)
2315    (progn
2316      (insert
2317       gnus-tmp-opening-bracket
2318       (format "%4d: %-20s"
2319               gnus-tmp-lines
2320               (if (> (length gnus-tmp-name) 20)
2321                   (substring gnus-tmp-name 0 20)
2322                 gnus-tmp-name))
2323       gnus-tmp-closing-bracket)
2324      (point))
2325    gnus-mouse-face-prop gnus-mouse-face)
2326   (insert " " gnus-tmp-subject-or-nil "\n"))
2327
2328 (defvar gnus-summary-line-format-spec
2329   (gnus-byte-code 'gnus-summary-line-format-spec))
2330
2331 (defun gnus-summary-dummy-line-format-spec ()
2332   (insert "*  ")
2333   (put-text-property
2334    (point)
2335    (progn
2336      (insert ":                          :")
2337      (point))
2338    gnus-mouse-face-prop gnus-mouse-face)
2339   (insert " " gnus-tmp-subject "\n"))
2340
2341 (defvar gnus-summary-dummy-line-format-spec
2342   (gnus-byte-code 'gnus-summary-dummy-line-format-spec))
2343
2344 (defun gnus-group-line-format-spec ()
2345   (insert gnus-tmp-marked-mark gnus-tmp-subscribed
2346           gnus-tmp-process-marked
2347           gnus-group-indentation
2348           (format "%5s: " gnus-tmp-number-of-unread))
2349   (put-text-property
2350    (point)
2351    (progn
2352      (insert gnus-tmp-group "\n")
2353      (1- (point)))
2354    gnus-mouse-face-prop gnus-mouse-face))
2355 (defvar gnus-group-line-format-spec
2356   (gnus-byte-code 'gnus-group-line-format-spec))
2357
2358 (defvar gnus-format-specs
2359   `((version . ,emacs-version)
2360     (group ,gnus-group-line-format ,gnus-group-line-format-spec)
2361     (summary-dummy ,gnus-summary-dummy-line-format
2362                    ,gnus-summary-dummy-line-format-spec)
2363     (summary ,gnus-summary-line-format ,gnus-summary-line-format-spec)))
2364
2365 (defvar gnus-article-mode-line-format-spec nil)
2366 (defvar gnus-summary-mode-line-format-spec nil)
2367 (defvar gnus-group-mode-line-format-spec nil)
2368
2369 ;;; Phew.  All that gruft is over, fortunately.
2370
2371 \f
2372 ;;;
2373 ;;; Gnus Utility Functions
2374 ;;;
2375
2376 (defun gnus-extract-address-components (from)
2377   (let (name address)
2378     ;; First find the address - the thing with the @ in it.  This may
2379     ;; not be accurate in mail addresses, but does the trick most of
2380     ;; the time in news messages.
2381     (if (string-match "\\b[^@ \t<>]+[!@][^@ \t<>]+\\b" from)
2382         (setq address (substring from (match-beginning 0) (match-end 0))))
2383     ;; Then we check whether the "name <address>" format is used.
2384     (and address
2385          ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>
2386          ;; Linear white space is not required.
2387          (string-match (concat "[ \t]*<" (regexp-quote address) ">") from)
2388          (and (setq name (substring from 0 (match-beginning 0)))
2389               ;; Strip any quotes from the name.
2390               (string-match "\".*\"" name)
2391               (setq name (substring name 1 (1- (match-end 0))))))
2392     ;; If not, then "address (name)" is used.
2393     (or name
2394         (and (string-match "(.+)" from)
2395              (setq name (substring from (1+ (match-beginning 0))
2396                                    (1- (match-end 0)))))
2397         (and (string-match "()" from)
2398              (setq name address))
2399         ;; Fix by MORIOKA Tomohiko <morioka@jaist.ac.jp>.
2400         ;; XOVER might not support folded From headers.
2401         (and (string-match "(.*" from)
2402              (setq name (substring from (1+ (match-beginning 0))
2403                                    (match-end 0)))))
2404     ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
2405     (list (or name from) (or address from))))
2406
2407 (defun gnus-fetch-field (field)
2408   "Return the value of the header FIELD of current article."
2409   (save-excursion
2410     (save-restriction
2411       (let ((case-fold-search t))
2412         (nnheader-narrow-to-headers)
2413         (mail-fetch-field field)))))
2414
2415 (defun gnus-goto-colon ()
2416   (beginning-of-line)
2417   (search-forward ":" (gnus-point-at-eol) t))
2418
2419 ;;;###autoload
2420 (defun gnus-update-format (var)
2421   "Update the format specification near point."
2422   (interactive
2423    (list
2424     (save-excursion
2425       (eval-defun nil)
2426       ;; Find the end of the current word.
2427       (re-search-forward "[ \t\n]" nil t)
2428       ;; Search backward.
2429       (when (re-search-backward "\\(gnus-[-a-z]+-line-format\\)" nil t)
2430         (match-string 1)))))
2431   (let* ((type (intern (progn (string-match "gnus-\\([-a-z]+\\)-line" var)
2432                               (match-string 1 var))))
2433          (entry (assq type gnus-format-specs))
2434          value spec)
2435     (when entry
2436       (setq gnus-format-specs (delq entry gnus-format-specs)))
2437     (set
2438      (intern (format "%s-spec" var))
2439      (gnus-parse-format (setq value (symbol-value (intern var)))
2440                         (symbol-value (intern (format "%s-alist" var)))
2441                         (not (string-match "mode" var))))
2442     (setq spec (symbol-value (intern (format "%s-spec" var))))
2443     (push (list type value spec) gnus-format-specs)
2444
2445     (pop-to-buffer "*Gnus Format*")
2446     (erase-buffer)
2447     (lisp-interaction-mode)
2448     (insert (pp-to-string spec))))
2449
2450 (defun gnus-update-format-specifications (&optional force)
2451   "Update all (necessary) format specifications."
2452   ;; Make the indentation array.
2453   (gnus-make-thread-indent-array)
2454
2455   ;; See whether all the stored info needs to be flushed.
2456   (when (or force
2457             (not (equal emacs-version
2458                         (cdr (assq 'version gnus-format-specs)))))
2459     (setq gnus-format-specs nil))
2460
2461   ;; Go through all the formats and see whether they need updating.
2462   (let ((types '(summary summary-dummy group
2463                          summary-mode group-mode article-mode))
2464         new-format entry type val)
2465     (while (setq type (pop types))
2466       ;; Jump to the proper buffer to find out the value of
2467       ;; the variable, if possible.  (It may be buffer-local.)
2468       (save-excursion
2469         (let ((buffer (intern (format "gnus-%s-buffer" type)))
2470               val)
2471           (when (and (boundp buffer)
2472                      (setq val (symbol-value buffer))
2473                      (get-buffer val)
2474                      (buffer-name (get-buffer val)))
2475             (set-buffer (get-buffer val)))
2476           (setq new-format (symbol-value
2477                             (intern (format "gnus-%s-line-format" type))))))
2478       (setq entry (cdr (assq type gnus-format-specs)))
2479       (if (and entry
2480                (equal (car entry) new-format))
2481           ;; Use the old format.
2482           (set (intern (format "gnus-%s-line-format-spec" type))
2483                (cadr entry))
2484         ;; This is a new format.
2485         (setq val
2486               (if (not (stringp new-format))
2487                   ;; This is a function call or something.
2488                   new-format
2489                 ;; This is a "real" format.
2490                 (gnus-parse-format
2491                  new-format
2492                  (symbol-value
2493                   (intern (format "gnus-%s-line-format-alist"
2494                                   (if (eq type 'article-mode)
2495                                       'summary-mode type))))
2496                  (not (string-match "mode$" (symbol-name type))))))
2497         ;; Enter the new format spec into the list.
2498         (if entry
2499             (progn
2500               (setcar (cdr entry) val)
2501               (setcar entry new-format))
2502           (push (list type new-format val) gnus-format-specs))
2503         (set (intern (format "gnus-%s-line-format-spec" type)) val))))
2504
2505   (gnus-update-group-mark-positions)
2506   (gnus-update-summary-mark-positions)
2507
2508   ;; See whether we need to read the description file.
2509   (if (and (string-match "%[-,0-9]*D" gnus-group-line-format)
2510            (not gnus-description-hashtb)
2511            gnus-read-active-file)
2512       (gnus-read-all-descriptions-files)))
2513
2514 (defun gnus-update-summary-mark-positions ()
2515   "Compute where the summary marks are to go."
2516   (save-excursion
2517     (let ((gnus-replied-mark 129)
2518           (gnus-score-below-mark 130)
2519           (gnus-score-over-mark 130)
2520           (thread nil)
2521           (gnus-visual nil)
2522           pos)
2523       (gnus-set-work-buffer)
2524       (gnus-summary-insert-line
2525        [0 "" "" "" "" "" 0 0 ""]  0 nil 128 t nil "" nil 1)
2526       (goto-char (point-min))
2527       (setq pos (list (cons 'unread (and (search-forward "\200" nil t)
2528                                          (- (point) 2)))))
2529       (goto-char (point-min))
2530       (push (cons 'replied (and (search-forward "\201" nil t) (- (point) 2)))
2531             pos)
2532       (goto-char (point-min))
2533       (push (cons 'score (and (search-forward "\202" nil t) (- (point) 2)))
2534             pos)
2535       (setq gnus-summary-mark-positions pos))))
2536
2537 (defun gnus-update-group-mark-positions ()
2538   (save-excursion
2539     (let ((gnus-process-mark 128)
2540           (gnus-group-marked '("dummy.group")))
2541       (gnus-set-active "dummy.group" '(0 . 0))
2542       (gnus-set-work-buffer)
2543       (gnus-group-insert-group-line "dummy.group" 0 nil 0 nil)
2544       (goto-char (point-min))
2545       (setq gnus-group-mark-positions
2546             (list (cons 'process (and (search-forward "\200" nil t)
2547                                       (- (point) 2))))))))
2548
2549 (defvar gnus-mouse-face-0 'highlight)
2550 (defvar gnus-mouse-face-1 'highlight)
2551 (defvar gnus-mouse-face-2 'highlight)
2552 (defvar gnus-mouse-face-3 'highlight)
2553 (defvar gnus-mouse-face-4 'highlight)
2554
2555 (defun gnus-mouse-face-function (form type)
2556   `(put-text-property
2557     (point) (progn ,@form (point))
2558     gnus-mouse-face-prop
2559     ,(if (equal type 0)
2560          'gnus-mouse-face
2561        `(quote ,(symbol-value (intern (format "gnus-mouse-face-%d" type)))))))
2562
2563 (defvar gnus-face-0 'bold)
2564 (defvar gnus-face-1 'italic)
2565 (defvar gnus-face-2 'bold-italic)
2566 (defvar gnus-face-3 'bold)
2567 (defvar gnus-face-4 'bold)
2568
2569 (defun gnus-face-face-function (form type)
2570   `(put-text-property
2571     (point) (progn ,@form (point))
2572     'face ',(symbol-value (intern (format "gnus-face-%d" type)))))
2573
2574 (defun gnus-max-width-function (el max-width)
2575   (or (numberp max-width) (signal 'wrong-type-argument '(numberp max-width)))
2576   (if (symbolp el)
2577       `(if (> (length ,el) ,max-width)
2578            (substring ,el 0 ,max-width)
2579          ,el)
2580     `(let ((val (eval ,el)))
2581        (if (numberp val)
2582            (setq val (int-to-string val)))
2583        (if (> (length val) ,max-width)
2584            (substring val 0 ,max-width)
2585          val))))
2586
2587 (defun gnus-parse-format (format spec-alist &optional insert)
2588   ;; This function parses the FORMAT string with the help of the
2589   ;; SPEC-ALIST and returns a list that can be eval'ed to return the
2590   ;; string.  If the FORMAT string contains the specifiers %( and %)
2591   ;; the text between them will have the mouse-face text property.
2592   (if (string-match
2593        "\\`\\(.*\\)%[0-9]?[{(]\\(.*\\)%[0-9]?[})]\\(.*\n?\\)\\'"
2594        format)
2595       (gnus-parse-complex-format format spec-alist)
2596     ;; This is a simple format.
2597     (gnus-parse-simple-format format spec-alist insert)))
2598
2599 (defun gnus-parse-complex-format (format spec-alist)
2600   (save-excursion
2601     (gnus-set-work-buffer)
2602     (insert format)
2603     (goto-char (point-min))
2604     (while (re-search-forward "\"" nil t)
2605       (replace-match "\\\"" nil t))
2606     (goto-char (point-min))
2607     (insert "(\"")
2608     (while (re-search-forward "%\\([0-9]+\\)?\\([{}()]\\)" nil t)
2609       (let ((number (if (match-beginning 1)
2610                         (match-string 1) "0"))
2611             (delim (aref (match-string 2) 0)))
2612         (if (or (= delim ?\() (= delim ?\{))
2613             (replace-match (concat "\"(" (if (= delim ?\() "mouse" "face")
2614                                    " " number " \""))
2615           (replace-match "\")\""))))
2616     (goto-char (point-max))
2617     (insert "\")")
2618     (goto-char (point-min))
2619     (let ((form (read (current-buffer))))
2620       (cons 'progn (gnus-complex-form-to-spec form spec-alist)))))
2621
2622 (defun gnus-complex-form-to-spec (form spec-alist)
2623   (delq nil
2624         (mapcar
2625          (lambda (sform)
2626            (if (stringp sform)
2627                (gnus-parse-simple-format sform spec-alist t)
2628              (funcall (intern (format "gnus-%s-face-function" (car sform)))
2629                       (gnus-complex-form-to-spec (cddr sform) spec-alist)
2630                       (nth 1 sform))))
2631          form)))
2632
2633 (defun gnus-parse-simple-format (format spec-alist &optional insert)
2634   ;; This function parses the FORMAT string with the help of the
2635   ;; SPEC-ALIST and returns a list that can be eval'ed to return a
2636   ;; string.
2637   (let ((max-width 0)
2638         spec flist fstring newspec elem beg result dontinsert)
2639     (save-excursion
2640       (gnus-set-work-buffer)
2641       (insert format)
2642       (goto-char (point-min))
2643       (while (re-search-forward "%[-0-9]*\\(,[0-9]+\\)?\\([^0-9]\\)\\(.\\)?"
2644                                 nil t)
2645         (if (= (setq spec (string-to-char (match-string 2))) ?%)
2646               (setq newspec "%"
2647                     beg (1+ (match-beginning 0)))
2648           ;; First check if there are any specs that look anything like
2649           ;; "%12,12A", ie. with a "max width specification".  These have
2650           ;; to be treated specially.
2651           (if (setq beg (match-beginning 1))
2652               (setq max-width
2653                     (string-to-int
2654                      (buffer-substring
2655                       (1+ (match-beginning 1)) (match-end 1))))
2656             (setq max-width 0)
2657             (setq beg (match-beginning 2)))
2658           ;; Find the specification from `spec-alist'.
2659           (unless (setq elem (cdr (assq spec spec-alist)))
2660             (setq elem '("*" ?s)))
2661           ;; Treat user defined format specifiers specially.
2662           (when (eq (car elem) 'gnus-tmp-user-defined)
2663             (setq elem
2664                   (list
2665                    (list (intern (concat "gnus-user-format-function-"
2666                                          (match-string 3)))
2667                          'gnus-tmp-header) ?s))
2668             (delete-region (match-beginning 3) (match-end 3)))
2669           (if (not (zerop max-width))
2670               (let ((el (car elem)))
2671                 (cond ((= (cadr elem) ?c)
2672                        (setq el (list 'char-to-string el)))
2673                       ((= (cadr elem) ?d)
2674                        (setq el (list 'int-to-string el))))
2675                 (setq flist (cons (gnus-max-width-function el max-width)
2676                                   flist))
2677                 (setq newspec ?s))
2678             (progn
2679               (setq flist (cons (car elem) flist))
2680               (setq newspec (cadr elem)))))
2681         ;; Remove the old specification (and possibly a ",12" string).
2682         (delete-region beg (match-end 2))
2683         ;; Insert the new specification.
2684         (goto-char beg)
2685         (insert newspec))
2686       (setq fstring (buffer-substring 1 (point-max))))
2687     ;; Do some postprocessing to increase efficiency.
2688     (setq
2689      result
2690      (cond
2691       ;; Emptyness.
2692       ((string= fstring "")
2693        nil)
2694       ;; Not a format string.
2695       ((not (string-match "%" fstring))
2696        (list fstring))
2697       ;; A format string with just a single string spec.
2698       ((string= fstring "%s")
2699        (list (car flist)))
2700       ;; A single character.
2701       ((string= fstring "%c")
2702        (list (car flist)))
2703       ;; A single number.
2704       ((string= fstring "%d")
2705        (setq dontinsert)
2706        (if insert
2707            (list `(princ ,(car flist)))
2708          (list `(int-to-string ,(car flist)))))
2709       ;; Just lots of chars and strings.
2710       ((string-match "\\`\\(%[cs]\\)+\\'" fstring)
2711        (nreverse flist))
2712       ;; A single string spec at the beginning of the spec.
2713       ((string-match "\\`%[sc][^%]+\\'" fstring)
2714        (list (car flist) (substring fstring 2)))
2715       ;; A single string spec in the middle of the spec.
2716       ((string-match "\\`\\([^%]+\\)%[sc]\\([^%]+\\)\\'" fstring)
2717        (list (match-string 1 fstring) (car flist) (match-string 2 fstring)))
2718       ;; A single string spec in the end of the spec.
2719       ((string-match "\\`\\([^%]+\\)%[sc]\\'" fstring)
2720        (list (match-string 1 fstring) (car flist)))
2721       ;; A more complex spec.
2722       (t
2723        (list (cons 'format (cons fstring (nreverse flist)))))))
2724
2725     (if insert
2726         (when result
2727           (if dontinsert
2728               result
2729             (cons 'insert result)))
2730       (cond ((stringp result)
2731              result)
2732             ((consp result)
2733              (cons 'concat result))
2734             (t "")))))
2735
2736 (defun gnus-eval-format (format &optional alist props)
2737   "Eval the format variable FORMAT, using ALIST.
2738 If PROPS, insert the result."
2739   (let ((form (gnus-parse-format format alist props)))
2740     (if props
2741         (add-text-properties (point) (progn (eval form) (point)) props)
2742       (eval form))))
2743
2744 (defun gnus-remove-text-with-property (prop)
2745   "Delete all text in the current buffer with text property PROP."
2746   (save-excursion
2747     (goto-char (point-min))
2748     (while (not (eobp))
2749       (while (get-text-property (point) prop)
2750         (delete-char 1))
2751       (goto-char (next-single-property-change (point) prop nil (point-max))))))
2752
2753 (defun gnus-set-work-buffer ()
2754   (if (get-buffer gnus-work-buffer)
2755       (progn
2756         (set-buffer gnus-work-buffer)
2757         (erase-buffer))
2758     (set-buffer (get-buffer-create gnus-work-buffer))
2759     (kill-all-local-variables)
2760     (buffer-disable-undo (current-buffer))
2761     (gnus-add-current-to-buffer-list)))
2762
2763 ;; Article file names when saving.
2764
2765 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2766   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2767 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
2768 Otherwise, it is like ~/News/news/group/num."
2769   (let ((default
2770           (expand-file-name
2771            (concat (if (gnus-use-long-file-name 'not-save)
2772                        (gnus-capitalize-newsgroup newsgroup)
2773                      (gnus-newsgroup-directory-form newsgroup))
2774                    "/" (int-to-string (mail-header-number headers)))
2775            (or gnus-article-save-directory "~/News"))))
2776     (if (and last-file
2777              (string-equal (file-name-directory default)
2778                            (file-name-directory last-file))
2779              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2780         default
2781       (or last-file default))))
2782
2783 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2784   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2785 If variable `gnus-use-long-file-name' is non-nil, it is
2786 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2787   (let ((default
2788           (expand-file-name
2789            (concat (if (gnus-use-long-file-name 'not-save)
2790                        newsgroup
2791                      (gnus-newsgroup-directory-form newsgroup))
2792                    "/" (int-to-string (mail-header-number headers)))
2793            (or gnus-article-save-directory "~/News"))))
2794     (if (and last-file
2795              (string-equal (file-name-directory default)
2796                            (file-name-directory last-file))
2797              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2798         default
2799       (or last-file default))))
2800
2801 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2802   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2803 If variable `gnus-use-long-file-name' is non-nil, it is
2804 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2805   (or last-file
2806       (expand-file-name
2807        (if (gnus-use-long-file-name 'not-save)
2808            (gnus-capitalize-newsgroup newsgroup)
2809          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2810        (or gnus-article-save-directory "~/News"))))
2811
2812 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2813   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2814 If variable `gnus-use-long-file-name' is non-nil, it is
2815 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2816   (or last-file
2817       (expand-file-name
2818        (if (gnus-use-long-file-name 'not-save)
2819            newsgroup
2820          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2821        (or gnus-article-save-directory "~/News"))))
2822
2823 ;; For subscribing new newsgroup
2824
2825 (defun gnus-subscribe-hierarchical-interactive (groups)
2826   (let ((groups (sort groups 'string<))
2827         prefixes prefix start ans group starts)
2828     (while groups
2829       (setq prefixes (list "^"))
2830       (while (and groups prefixes)
2831         (while (not (string-match (car prefixes) (car groups)))
2832           (setq prefixes (cdr prefixes)))
2833         (setq prefix (car prefixes))
2834         (setq start (1- (length prefix)))
2835         (if (and (string-match "[^\\.]\\." (car groups) start)
2836                  (cdr groups)
2837                  (setq prefix
2838                        (concat "^" (substring (car groups) 0 (match-end 0))))
2839                  (string-match prefix (cadr groups)))
2840             (progn
2841               (setq prefixes (cons prefix prefixes))
2842               (message "Descend hierarchy %s? ([y]nsq): "
2843                        (substring prefix 1 (1- (length prefix))))
2844               (setq ans (read-char))
2845               (cond ((= ans ?n)
2846                      (while (and groups
2847                                  (string-match prefix
2848                                                (setq group (car groups))))
2849                        (setq gnus-killed-list
2850                              (cons group gnus-killed-list))
2851                        (gnus-sethash group group gnus-killed-hashtb)
2852                        (setq groups (cdr groups)))
2853                      (setq starts (cdr starts)))
2854                     ((= ans ?s)
2855                      (while (and groups
2856                                  (string-match prefix
2857                                                (setq group (car groups))))
2858                        (gnus-sethash group group gnus-killed-hashtb)
2859                        (gnus-subscribe-alphabetically (car groups))
2860                        (setq groups (cdr groups)))
2861                      (setq starts (cdr starts)))
2862                     ((= ans ?q)
2863                      (while groups
2864                        (setq group (car groups))
2865                        (setq gnus-killed-list (cons group gnus-killed-list))
2866                        (gnus-sethash group group gnus-killed-hashtb)
2867                        (setq groups (cdr groups))))
2868                     (t nil)))
2869           (message "Subscribe %s? ([n]yq)" (car groups))
2870           (setq ans (read-char))
2871           (setq group (car groups))
2872           (cond ((= ans ?y)
2873                  (gnus-subscribe-alphabetically (car groups))
2874                  (gnus-sethash group group gnus-killed-hashtb))
2875                 ((= ans ?q)
2876                  (while groups
2877                    (setq group (car groups))
2878                    (setq gnus-killed-list (cons group gnus-killed-list))
2879                    (gnus-sethash group group gnus-killed-hashtb)
2880                    (setq groups (cdr groups))))
2881                 (t
2882                  (setq gnus-killed-list (cons group gnus-killed-list))
2883                  (gnus-sethash group group gnus-killed-hashtb)))
2884           (setq groups (cdr groups)))))))
2885
2886 (defun gnus-subscribe-randomly (newsgroup)
2887   "Subscribe new NEWSGROUP by making it the first newsgroup."
2888   (gnus-subscribe-newsgroup newsgroup))
2889
2890 (defun gnus-subscribe-alphabetically (newgroup)
2891   "Subscribe new NEWSGROUP and insert it in alphabetical order."
2892   (let ((groups (cdr gnus-newsrc-alist))
2893         before)
2894     (while (and (not before) groups)
2895       (if (string< newgroup (caar groups))
2896           (setq before (caar groups))
2897         (setq groups (cdr groups))))
2898     (gnus-subscribe-newsgroup newgroup before)))
2899
2900 (defun gnus-subscribe-hierarchically (newgroup)
2901   "Subscribe new NEWSGROUP and insert it in hierarchical newsgroup order."
2902   ;; Basic ideas by mike-w@cs.aukuni.ac.nz (Mike Williams)
2903   (save-excursion
2904     (set-buffer (find-file-noselect gnus-current-startup-file))
2905     (let ((groupkey newgroup)
2906           before)
2907       (while (and (not before) groupkey)
2908         (goto-char (point-min))
2909         (let ((groupkey-re
2910                (concat "^\\(" (regexp-quote groupkey) ".*\\)[!:]")))
2911           (while (and (re-search-forward groupkey-re nil t)
2912                       (progn
2913                         (setq before (match-string 1))
2914                         (string< before newgroup)))))
2915         ;; Remove tail of newsgroup name (eg. a.b.c -> a.b)
2916         (setq groupkey
2917               (if (string-match "^\\(.*\\)\\.[^.]+$" groupkey)
2918                   (substring groupkey (match-beginning 1) (match-end 1)))))
2919       (gnus-subscribe-newsgroup newgroup before))))
2920
2921 (defun gnus-subscribe-interactively (group)
2922   "Subscribe the new GROUP interactively.
2923 It is inserted in hierarchical newsgroup order if subscribed.  If not,
2924 it is killed."
2925   (if (gnus-y-or-n-p (format "Subscribe new newsgroup: %s " group))
2926       (gnus-subscribe-hierarchically group)
2927     (push group gnus-killed-list)))
2928
2929 (defun gnus-subscribe-zombies (group)
2930   "Make the new GROUP into a zombie group."
2931   (push group gnus-zombie-list))
2932
2933 (defun gnus-subscribe-killed (group)
2934   "Make the new GROUP a killed group."
2935   (push group gnus-killed-list))
2936
2937 (defun gnus-subscribe-newsgroup (newsgroup &optional next)
2938   "Subscribe new NEWSGROUP.
2939 If NEXT is non-nil, it is inserted before NEXT.  Otherwise it is made
2940 the first newsgroup."
2941   ;; We subscribe the group by changing its level to `subscribed'.
2942   (gnus-group-change-level
2943    newsgroup gnus-level-default-subscribed
2944    gnus-level-killed (gnus-gethash (or next "dummy.group") gnus-newsrc-hashtb))
2945   (gnus-message 5 "Subscribe newsgroup: %s" newsgroup))
2946
2947 ;; For directories
2948
2949 (defun gnus-newsgroup-directory-form (newsgroup)
2950   "Make hierarchical directory name from NEWSGROUP name."
2951   (let ((newsgroup (gnus-newsgroup-savable-name newsgroup))
2952         (len (length newsgroup))
2953         idx)
2954     ;; If this is a foreign group, we don't want to translate the
2955     ;; entire name.
2956     (if (setq idx (string-match ":" newsgroup))
2957         (aset newsgroup idx ?/)
2958       (setq idx 0))
2959     ;; Replace all occurrences of `.' with `/'.
2960     (while (< idx len)
2961       (if (= (aref newsgroup idx) ?.)
2962           (aset newsgroup idx ?/))
2963       (setq idx (1+ idx)))
2964     newsgroup))
2965
2966 (defun gnus-newsgroup-savable-name (group)
2967   ;; Replace any slashes in a group name (eg. an ange-ftp nndoc group)
2968   ;; with dots.
2969   (nnheader-replace-chars-in-string group ?/ ?.))
2970
2971 (defun gnus-make-directory (dir)
2972   "Make DIRECTORY recursively."
2973   ;; Why don't we use `(make-directory dir 'parents)'?  That's just one
2974   ;; of the many mysteries of the universe.
2975   (let* ((dir (expand-file-name dir default-directory))
2976          dirs err)
2977     (if (string-match "/$" dir)
2978         (setq dir (substring dir 0 (match-beginning 0))))
2979     ;; First go down the path until we find a directory that exists.
2980     (while (not (file-exists-p dir))
2981       (setq dirs (cons dir dirs))
2982       (string-match "/[^/]+$" dir)
2983       (setq dir (substring dir 0 (match-beginning 0))))
2984     ;; Then create all the subdirs.
2985     (while (and dirs (not err))
2986       (condition-case ()
2987           (make-directory (car dirs))
2988         (error (setq err t)))
2989       (setq dirs (cdr dirs)))
2990     ;; We return whether we were successful or not.
2991     (not dirs)))
2992
2993 (defun gnus-capitalize-newsgroup (newsgroup)
2994   "Capitalize NEWSGROUP name."
2995   (and (not (zerop (length newsgroup)))
2996        (concat (char-to-string (upcase (aref newsgroup 0)))
2997                (substring newsgroup 1))))
2998
2999 ;; Various... things.
3000
3001 (defun gnus-simplify-subject (subject &optional re-only)
3002   "Remove `Re:' and words in parentheses.
3003 If RE-ONLY is non-nil, strip leading `Re:'s only."
3004   (let ((case-fold-search t))           ;Ignore case.
3005     ;; Remove `Re:', `Re^N:', `Re(n)', and `Re[n]:'.
3006     (when (string-match "\\`\\(re\\([[(^][0-9]+[])]?\\)?:[ \t]*\\)+" subject)
3007       (setq subject (substring subject (match-end 0))))
3008     ;; Remove uninteresting prefixes.
3009     (if (and (not re-only)
3010              gnus-simplify-ignored-prefixes
3011              (string-match gnus-simplify-ignored-prefixes subject))
3012         (setq subject (substring subject (match-end 0))))
3013     ;; Remove words in parentheses from end.
3014     (unless re-only
3015       (while (string-match "[ \t\n]*([^()]*)[ \t\n]*\\'" subject)
3016         (setq subject (substring subject 0 (match-beginning 0)))))
3017     ;; Return subject string.
3018     subject))
3019
3020 ;; Remove any leading "re:"s, any trailing paren phrases, and simplify
3021 ;; all whitespace.
3022 ;; Written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
3023 (defun gnus-simplify-buffer-fuzzy ()
3024   (goto-char (point-min))
3025   (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*" nil t)
3026   (goto-char (match-beginning 0))
3027   (while (or
3028           (looking-at "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*")
3029           (looking-at "^[[].*:[ \t].*[]]$"))
3030     (goto-char (point-min))
3031     (while (re-search-forward "^[ \t]*\\(re\\|fwd\\)[[{(^0-9]*[])}]?[:;][ \t]*"
3032                               nil t)
3033       (replace-match "" t t))
3034     (goto-char (point-min))
3035     (while (re-search-forward "^[[].*:[ \t].*[]]$" nil t)
3036       (goto-char (match-end 0))
3037       (delete-char -1)
3038       (delete-region
3039        (progn (goto-char (match-beginning 0)))
3040        (re-search-forward ":"))))
3041   (goto-char (point-min))
3042   (while (re-search-forward "[ \t\n]*[[{(][^()]*[]})][ \t]*$" nil t)
3043     (replace-match "" t t))
3044   (goto-char (point-min))
3045   (while (re-search-forward "[ \t]+" nil t)
3046     (replace-match " " t t))
3047   (goto-char (point-min))
3048   (while (re-search-forward "[ \t]+$" nil t)
3049     (replace-match "" t t))
3050   (goto-char (point-min))
3051   (while (re-search-forward "^[ \t]+" nil t)
3052     (replace-match "" t t))
3053   (goto-char (point-min))
3054   (if gnus-simplify-subject-fuzzy-regexp
3055       (if (listp gnus-simplify-subject-fuzzy-regexp)
3056           (let ((list gnus-simplify-subject-fuzzy-regexp))
3057             (while list
3058               (goto-char (point-min))
3059               (while (re-search-forward (car list) nil t)
3060                 (replace-match "" t t))
3061               (setq list (cdr list))))
3062         (while (re-search-forward gnus-simplify-subject-fuzzy-regexp nil t)
3063           (replace-match "" t t)))))
3064
3065 (defun gnus-simplify-subject-fuzzy (subject)
3066   "Siplify a subject string fuzzily."
3067   (save-excursion
3068     (gnus-set-work-buffer)
3069     (let ((case-fold-search t))
3070       (insert subject)
3071       (inline (gnus-simplify-buffer-fuzzy))
3072       (buffer-string))))
3073
3074 ;; Add the current buffer to the list of buffers to be killed on exit.
3075 (defun gnus-add-current-to-buffer-list ()
3076   (or (memq (current-buffer) gnus-buffer-list)
3077       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
3078
3079 (defun gnus-string> (s1 s2)
3080   (not (or (string< s1 s2)
3081            (string= s1 s2))))
3082
3083 ;;; General various misc type functions.
3084
3085 (defun gnus-clear-system ()
3086   "Clear all variables and buffers."
3087   ;; Clear Gnus variables.
3088   (let ((variables gnus-variable-list))
3089     (while variables
3090       (set (car variables) nil)
3091       (setq variables (cdr variables))))
3092   ;; Clear other internal variables.
3093   (setq gnus-list-of-killed-groups nil
3094         gnus-have-read-active-file nil
3095         gnus-newsrc-alist nil
3096         gnus-newsrc-hashtb nil
3097         gnus-killed-list nil
3098         gnus-zombie-list nil
3099         gnus-killed-hashtb nil
3100         gnus-active-hashtb nil
3101         gnus-moderated-list nil
3102         gnus-description-hashtb nil
3103         gnus-newsgroup-headers nil
3104         gnus-newsgroup-name nil
3105         gnus-server-alist nil
3106         gnus-opened-servers nil
3107         gnus-current-select-method nil)
3108   ;; Reset any score variables.
3109   (when gnus-use-scoring 
3110     (gnus-score-close))
3111   ;; Kill the startup file.
3112   (and gnus-current-startup-file
3113        (get-file-buffer gnus-current-startup-file)
3114        (kill-buffer (get-file-buffer gnus-current-startup-file)))
3115   ;; Save any cache buffers.
3116   (when gnus-use-cache 
3117     (gnus-cache-save-buffers))
3118   ;; Clear the dribble buffer.
3119   (gnus-dribble-clear)
3120   ;; Close down NoCeM.
3121   (when gnus-use-nocem 
3122     (gnus-nocem-close))
3123   ;; Shut down the demons.
3124   (when gnus-use-demon
3125     (gnus-demon-cancel))
3126   ;; Kill global KILL file buffer.
3127   (when (get-file-buffer (gnus-newsgroup-kill-file nil))
3128     (kill-buffer (get-file-buffer (gnus-newsgroup-kill-file nil))))
3129   (gnus-kill-buffer nntp-server-buffer)
3130   ;; Backlog.
3131   (when gnus-keep-backlog
3132     (gnus-backlog-shutdown))
3133   ;; Kill Gnus buffers.
3134   (while gnus-buffer-list
3135     (gnus-kill-buffer (pop gnus-buffer-list)))
3136   ;; Remove Gnus frames.
3137   (while gnus-created-frames
3138     (when (frame-live-p (car gnus-created-frames))
3139       ;; We slap a condition-case around this `delete-frame' to ensure 
3140       ;; agains errors if we try do delete the single frame that's left.
3141       (condition-case ()
3142           (delete-frame (car gnus-created-frames))
3143         (error nil)))
3144     (pop gnus-created-frames)))
3145
3146 (defun gnus-windows-old-to-new (setting)
3147   ;; First we take care of the really, really old Gnus 3 actions.
3148   (when (symbolp setting)
3149     (setq setting
3150           ;; Take care of ooold GNUS 3.x values.
3151           (cond ((eq setting 'SelectArticle) 'article)
3152                 ((memq setting '(SelectSubject ExpandSubject)) 'summary)
3153                 ((memq setting '(SelectNewsgroup ExitNewsgroup)) 'group)
3154                 (t setting))))
3155   (if (or (listp setting)
3156           (not (and gnus-window-configuration
3157                     (memq setting '(group summary article)))))
3158       setting
3159     (let* ((setting (if (eq setting 'group)
3160                         (if (assq 'newsgroup gnus-window-configuration)
3161                             'newsgroup
3162                           'newsgroups) setting))
3163            (elem (cadr (assq setting gnus-window-configuration)))
3164            (total (apply '+ elem))
3165            (types '(group summary article))
3166            (pbuf (if (eq setting 'newsgroups) 'group 'summary))
3167            (i 0)
3168            perc
3169            out)
3170       (while (< i 3)
3171         (or (not (numberp (nth i elem)))
3172             (zerop (nth i elem))
3173             (progn
3174               (setq perc  (/ (float (nth 0 elem)) total))
3175               (setq out (cons (if (eq pbuf (nth i types))
3176                                   (vector (nth i types) perc 'point)
3177                                 (vector (nth i types) perc))
3178                               out))))
3179         (setq i (1+ i)))
3180       (list (nreverse out)))))
3181
3182 ;;;###autoload
3183 (defun gnus-add-configuration (conf)
3184   "Add the window configuration CONF to `gnus-buffer-configuration'."
3185   (setq gnus-buffer-configuration
3186         (cons conf (delq (assq (car conf) gnus-buffer-configuration)
3187                          gnus-buffer-configuration))))
3188
3189 (defvar gnus-frame-list nil)
3190
3191 (defun gnus-configure-frame (split &optional window)
3192   "Split WINDOW according to SPLIT."
3193   (unless window
3194     (setq window (get-buffer-window (current-buffer))))
3195   (select-window window)
3196   ;; This might be an old-stylee buffer config.
3197   (when (vectorp split)
3198     (setq split (append split nil)))
3199   (when (or (consp (car split))
3200             (vectorp (car split)))
3201     (push 1.0 split)
3202     (push 'vertical split))
3203   ;; The SPLIT might be something that is to be evaled to
3204   ;; return a new SPLIT.
3205   (while (and (not (assq (car split) gnus-window-to-buffer))
3206               (gnus-functionp (car split)))
3207     (setq split (eval split)))
3208   (let* ((type (car split))
3209          (subs (cddr split))
3210          (len (if (eq type 'horizontal) (window-width) (window-height)))
3211          (total 0)
3212          (window-min-width (or gnus-window-min-width window-min-width))
3213          (window-min-height (or gnus-window-min-height window-min-height))
3214          s result new-win rest comp-subs size sub)
3215     (cond
3216      ;; Nothing to do here.
3217      ((null split))
3218      ;; Don't switch buffers.
3219      ((null type)
3220       (and (memq 'point split) window))
3221      ;; This is a buffer to be selected.
3222      ((not (memq type '(frame horizontal vertical)))
3223       (let ((buffer (cond ((stringp type) type)
3224                           (t (cdr (assq type gnus-window-to-buffer)))))
3225             buf)
3226         (unless buffer
3227           (error "Illegal buffer type: %s" type))
3228         (unless (setq buf (get-buffer (if (symbolp buffer)
3229                                           (symbol-value buffer) buffer)))
3230           (setq buf (get-buffer-create (if (symbolp buffer)
3231                                            (symbol-value buffer) buffer))))
3232         (switch-to-buffer buf)
3233         ;; We return the window if it has the `point' spec.
3234         (and (memq 'point split) window)))
3235      ;; This is a frame split.
3236      ((eq type 'frame)
3237       (unless gnus-frame-list
3238         (setq gnus-frame-list (list (window-frame
3239                                      (get-buffer-window (current-buffer))))))
3240       (let ((i 0)
3241             params frame fresult)
3242         (while (< i (length subs))
3243           ;; Frame parameter is gotten from the sub-split.
3244           (setq params (cadr (elt subs i)))
3245           ;; It should be a list.
3246           (unless (listp params)
3247             (setq params nil))
3248           ;; Create a new frame?
3249           (unless (setq frame (elt gnus-frame-list i))
3250             (nconc gnus-frame-list (list (setq frame (make-frame params))))
3251             (push frame gnus-created-frames))
3252           ;; Is the old frame still alive?
3253           (unless (frame-live-p frame)
3254             (setcar (nthcdr i gnus-frame-list)
3255                     (setq frame (make-frame params))))
3256           ;; Select the frame in question and do more splits there.
3257           (select-frame frame)
3258           (setq fresult (or (gnus-configure-frame (elt subs i)) fresult))
3259           (incf i))
3260         ;; Select the frame that has the selected buffer.
3261         (when fresult
3262           (select-frame (window-frame fresult)))))
3263      ;; This is a normal split.
3264      (t
3265       (when (> (length subs) 0)
3266         ;; First we have to compute the sizes of all new windows.
3267         (while subs
3268           (setq sub (append (pop subs) nil))
3269           (while (and (not (assq (car sub) gnus-window-to-buffer))
3270                       (gnus-functionp (car sub)))
3271             (setq sub (eval sub)))
3272           (when sub
3273             (push sub comp-subs)
3274             (setq size (cadar comp-subs))
3275             (cond ((equal size 1.0)
3276                    (setq rest (car comp-subs))
3277                    (setq s 0))
3278                   ((floatp size)
3279                    (setq s (floor (* size len))))
3280                   ((integerp size)
3281                    (setq s size))
3282                   (t
3283                    (error "Illegal size: %s" size)))
3284             ;; Try to make sure that we are inside the safe limits.
3285             (cond ((zerop s))
3286                   ((eq type 'horizontal)
3287                    (setq s (max s window-min-width)))
3288                   ((eq type 'vertical)
3289                    (setq s (max s window-min-height))))
3290             (setcar (cdar comp-subs) s)
3291             (incf total s)))
3292         ;; Take care of the "1.0" spec.
3293         (if rest
3294             (setcar (cdr rest) (- len total))
3295           (error "No 1.0 specs in %s" split))
3296         ;; The we do the actual splitting in a nice recursive
3297         ;; fashion.
3298         (setq comp-subs (nreverse comp-subs))
3299         (while comp-subs
3300           (if (null (cdr comp-subs))
3301               (setq new-win window)
3302             (setq new-win
3303                   (split-window window (cadar comp-subs)
3304                                 (eq type 'horizontal))))
3305           (setq result (or (gnus-configure-frame
3306                             (car comp-subs) window) result))
3307           (select-window new-win)
3308           (setq window new-win)
3309           (setq comp-subs (cdr comp-subs))))
3310       ;; Return the proper window, if any.
3311       (when result
3312         (select-window result))))))
3313
3314 (defvar gnus-frame-split-p nil)
3315
3316 (defun gnus-configure-windows (setting &optional force)
3317   (setq setting (gnus-windows-old-to-new setting))
3318   (let ((split (if (symbolp setting)
3319                    (cadr (assq setting gnus-buffer-configuration))
3320                  setting))
3321         (in-buf (current-buffer))
3322         rule val w height hor ohor heights sub jump-buffer
3323         rel total to-buf all-visible)
3324
3325     (setq gnus-frame-split-p nil)
3326
3327     (unless split
3328       (error "No such setting: %s" setting))
3329
3330     (if (and (setq all-visible (gnus-all-windows-visible-p split))
3331              (not force))
3332         ;; All the windows mentioned are already visible, so we just
3333         ;; put point in the assigned buffer, and do not touch the
3334         ;; winconf.
3335         (select-window all-visible)
3336
3337       ;; Either remove all windows or just remove all Gnus windows.
3338       (let ((frame (selected-frame)))
3339         (unwind-protect
3340             (if gnus-use-full-window
3341                 ;; We want to remove all other windows.
3342                 (if (not gnus-frame-split-p)
3343                     ;; This is not a `frame' split, so we ignore the
3344                     ;; other frames.  
3345                     (delete-other-windows)
3346                   ;; This is a `frame' split, so we delete all windows
3347                   ;; on all frames.
3348                   (mapcar 
3349                    (lambda (frame)
3350                      (unless (eq (cdr (assq 'minibuffer
3351                                             (frame-parameters frame)))
3352                                  'only)
3353                        (select-frame frame)
3354                        (delete-other-windows)))
3355                    (frame-list)))
3356               ;; Just remove some windows.
3357               (gnus-remove-some-windows)
3358               (switch-to-buffer nntp-server-buffer))
3359           (select-frame frame)))
3360
3361       (switch-to-buffer nntp-server-buffer)
3362       (gnus-configure-frame split (get-buffer-window (current-buffer))))))
3363
3364 (defun gnus-all-windows-visible-p (split)
3365   (when (vectorp split)
3366     (setq split (append split nil)))
3367   (when (or (consp (car split))
3368             (vectorp (car split)))
3369     (push 1.0 split)
3370     (push 'vertical split))
3371   ;; The SPLIT might be something that is to be evaled to
3372   ;; return a new SPLIT.
3373   (while (and (not (assq (car split) gnus-window-to-buffer))
3374               (gnus-functionp (car split)))
3375     (setq split (eval split)))
3376   (let* ((type (elt split 0)))
3377     (cond
3378      ((null split)
3379       t)
3380      ((not (or (eq type 'horizontal) (eq type 'vertical) (eq type 'frame)))
3381       (let ((buffer (cond ((stringp type) type)
3382                           (t (cdr (assq type gnus-window-to-buffer)))))
3383             win buf)
3384         (unless buffer
3385           (error "Illegal buffer type: %s" type))
3386         (when (setq buf (get-buffer (if (symbolp buffer) (symbol-value buffer)
3387                                       buffer)))
3388           (setq win (get-buffer-window buf t)))
3389         (when win
3390           (if (memq 'point split)
3391               win
3392             t))))
3393      (t
3394       (when (eq type 'frame)
3395         (setq gnus-frame-split-p t))
3396       (let ((n (mapcar 'gnus-all-windows-visible-p
3397                        (cddr split)))
3398             (win t))
3399         (while n
3400           (cond ((windowp (car n))
3401                  (setq win (car n)))
3402                 ((null (car n))
3403                  (setq win nil)))
3404           (setq n (cdr n)))
3405         win)))))
3406
3407 (defun gnus-window-top-edge (&optional window)
3408   (nth 1 (window-edges window)))
3409
3410 (defun gnus-remove-some-windows ()
3411   (let ((buffers gnus-window-to-buffer)
3412         buf bufs lowest-buf lowest)
3413     (save-excursion
3414       ;; Remove windows on all known Gnus buffers.
3415       (while buffers
3416         (setq buf (cdar buffers))
3417         (if (symbolp buf)
3418             (setq buf (and (boundp buf) (symbol-value buf))))
3419         (and buf
3420              (get-buffer-window buf)
3421              (progn
3422                (setq bufs (cons buf bufs))
3423                (pop-to-buffer buf)
3424                (if (or (not lowest)
3425                        (< (gnus-window-top-edge) lowest))
3426                    (progn
3427                      (setq lowest (gnus-window-top-edge))
3428                      (setq lowest-buf buf)))))
3429         (setq buffers (cdr buffers)))
3430       ;; Remove windows on *all* summary buffers.
3431       (let (wins)
3432         (walk-windows
3433          (lambda (win)
3434            (let ((buf (window-buffer win)))
3435              (if (string-match  "^\\*Summary" (buffer-name buf))
3436                  (progn
3437                    (setq bufs (cons buf bufs))
3438                    (pop-to-buffer buf)
3439                    (if (or (not lowest)
3440                            (< (gnus-window-top-edge) lowest))
3441                        (progn
3442                          (setq lowest-buf buf)
3443                          (setq lowest (gnus-window-top-edge))))))))))
3444       (and lowest-buf
3445            (progn
3446              (pop-to-buffer lowest-buf)
3447              (switch-to-buffer nntp-server-buffer)))
3448       (while bufs
3449         (and (not (eq (car bufs) lowest-buf))
3450              (delete-windows-on (car bufs)))
3451         (setq bufs (cdr bufs))))))
3452
3453 (defun gnus-version ()
3454   "Version numbers of this version of Gnus."
3455   (interactive)
3456   (let ((methods gnus-valid-select-methods)
3457         (mess gnus-version)
3458         meth)
3459     ;; Go through all the legal select methods and add their version
3460     ;; numbers to the total version string.  Only the backends that are
3461     ;; currently in use will have their message numbers taken into
3462     ;; consideration.
3463     (while methods
3464       (setq meth (intern (concat (caar methods) "-version")))
3465       (and (boundp meth)
3466            (stringp (symbol-value meth))
3467            (setq mess (concat mess "; " (symbol-value meth))))
3468       (setq methods (cdr methods)))
3469     (gnus-message 2 mess)))
3470
3471 (defun gnus-info-find-node ()
3472   "Find Info documentation of Gnus."
3473   (interactive)
3474   ;; Enlarge info window if needed.
3475   (let ((mode major-mode)
3476         gnus-info-buffer)
3477     (Info-goto-node (cadr (assq mode gnus-info-nodes)))
3478     (setq gnus-info-buffer (current-buffer))
3479     (gnus-configure-windows 'info)))
3480
3481 (defun gnus-days-between (date1 date2)
3482   ;; Return the number of days between date1 and date2.
3483   (- (gnus-day-number date1) (gnus-day-number date2)))
3484
3485 (defun gnus-day-number (date)
3486   (let ((dat (mapcar (lambda (s) (and s (string-to-int s)) )
3487                      (timezone-parse-date date))))
3488     (timezone-absolute-from-gregorian
3489      (nth 1 dat) (nth 2 dat) (car dat))))
3490
3491 (defun gnus-encode-date (date)
3492   "Convert DATE to internal time."
3493   (let* ((parse (timezone-parse-date date))
3494          (date (mapcar (lambda (d) (and d (string-to-int d))) parse))
3495          (time (mapcar 'string-to-int (timezone-parse-time (aref parse 3)))))
3496     (encode-time (caddr time) (cadr time) (car time)
3497                  (caddr date) (cadr date) (car date) (nth 4 date))))
3498
3499 (defun gnus-time-minus (t1 t2)
3500   "Subtract two internal times."
3501   (let ((borrow (< (cadr t1) (cadr t2))))
3502     (list (- (car t1) (car t2) (if borrow 1 0))
3503           (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
3504
3505 (defun gnus-file-newer-than (file date)
3506   (let ((fdate (nth 5 (file-attributes file))))
3507     (or (> (car fdate) (car date))
3508         (and (= (car fdate) (car date))
3509              (> (nth 1 fdate) (nth 1 date))))))
3510
3511 (defmacro gnus-local-set-keys (&rest plist)
3512   "Set the keys in PLIST in the current keymap."
3513   `(gnus-define-keys-1 (current-local-map) ',plist))
3514
3515 (defmacro gnus-define-keys (keymap &rest plist)
3516   "Define all keys in PLIST in KEYMAP."
3517   `(gnus-define-keys-1 (quote ,keymap) (quote ,plist)))
3518
3519 (put 'gnus-define-keys 'lisp-indent-function 1)
3520 (put 'gnus-define-keys 'lisp-indent-hook 1)
3521 (put 'gnus-define-keymap 'lisp-indent-function 1)
3522 (put 'gnus-define-keymap 'lisp-indent-hook 1)
3523
3524 (defmacro gnus-define-keymap (keymap &rest plist)
3525   "Define all keys in PLIST in KEYMAP."
3526   `(gnus-define-keys-1 ,keymap (quote ,plist)))
3527
3528 (defun gnus-define-keys-1 (keymap plist)
3529   (when (null keymap)
3530     (error "Can't set keys in a null keymap"))
3531   (cond ((symbolp keymap)
3532          (setq keymap (symbol-value keymap)))
3533         ((keymapp keymap))
3534         ((listp keymap)
3535          (set (car keymap) nil)
3536          (define-prefix-command (car keymap))
3537          (define-key (symbol-value (caddr keymap)) (cadr keymap) (car keymap))
3538          (setq keymap (symbol-value (car keymap)))))
3539   (let (key)
3540     (while plist
3541       (when (symbolp (setq key (pop plist)))
3542         (setq key (symbol-value key)))
3543       (define-key keymap key (pop plist)))))
3544
3545 (defun gnus-group-read-only-p (&optional group)
3546   "Check whether GROUP supports editing or not.
3547 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
3548 that that variable is buffer-local to the summary buffers."
3549   (let ((group (or group gnus-newsgroup-name)))
3550     (not (gnus-check-backend-function 'request-replace-article group))))
3551
3552 (defun gnus-group-total-expirable-p (group)
3553   "Check whether GROUP is total-expirable or not."
3554   (let ((params (gnus-info-params (gnus-get-info group))))
3555     (or (memq 'total-expire params)
3556         (cdr (assq 'total-expire params)) ; (total-expire . t)
3557         (and gnus-total-expirable-newsgroups ; Check var.
3558              (string-match gnus-total-expirable-newsgroups group)))))
3559
3560 (defun gnus-group-auto-expirable-p (group)
3561   "Check whether GROUP is total-expirable or not."
3562   (let ((params (gnus-info-params (gnus-get-info group))))
3563     (or (memq 'auto-expire params)
3564         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
3565         (and gnus-auto-expirable-newsgroups ; Check var.
3566              (string-match gnus-auto-expirable-newsgroups group)))))
3567
3568 (defun gnus-virtual-group-p (group)
3569   "Say whether GROUP is virtual or not."
3570   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
3571                         gnus-valid-select-methods)))
3572
3573 (defsubst gnus-simplify-subject-fully (subject)
3574   "Simplify a subject string according to the user's wishes."
3575   (cond
3576    ((null gnus-summary-gather-subject-limit)
3577     (gnus-simplify-subject-re subject))
3578    ((eq gnus-summary-gather-subject-limit 'fuzzy)
3579     (gnus-simplify-subject-fuzzy subject))
3580    ((numberp gnus-summary-gather-subject-limit)
3581     (gnus-limit-string (gnus-simplify-subject-re subject)
3582                        gnus-summary-gather-subject-limit))
3583    (t
3584     subject)))
3585
3586 (defsubst gnus-subject-equal (s1 s2 &optional simple-first)
3587   "Check whether two subjects are equal.  If optional argument
3588 simple-first is t, first argument is already simplified."
3589   (cond
3590    ((null simple-first)
3591     (equal (gnus-simplify-subject-fully s1)
3592            (gnus-simplify-subject-fully s2)))
3593    (t
3594     (equal s1
3595            (gnus-simplify-subject-fully s2)))))
3596
3597 ;; Returns a list of writable groups.
3598 (defun gnus-writable-groups ()
3599   (let ((alist gnus-newsrc-alist)
3600         groups group)
3601     (while (setq group (car (pop alist)))
3602       (unless (gnus-group-read-only-p group)
3603         (push group groups)))
3604     (nreverse groups)))
3605
3606 ;; Two silly functions to ensure that all `y-or-n-p' questions clear
3607 ;; the echo area.
3608 (defun gnus-y-or-n-p (prompt)
3609   (prog1
3610       (y-or-n-p prompt)
3611     (message "")))
3612
3613 (defun gnus-yes-or-no-p (prompt)
3614   (prog1
3615       (yes-or-no-p prompt)
3616     (message "")))
3617
3618 ;; Check whether to use long file names.
3619 (defun gnus-use-long-file-name (symbol)
3620   ;; The variable has to be set...
3621   (and gnus-use-long-file-name
3622        ;; If it isn't a list, then we return t.
3623        (or (not (listp gnus-use-long-file-name))
3624            ;; If it is a list, and the list contains `symbol', we
3625            ;; return nil.
3626            (not (memq symbol gnus-use-long-file-name)))))
3627
3628 ;; I suspect there's a better way, but I haven't taken the time to do
3629 ;; it yet. -erik selberg@cs.washington.edu
3630 (defun gnus-dd-mmm (messy-date)
3631   "Return a string like DD-MMM from a big messy string"
3632   (let ((datevec (timezone-parse-date messy-date)))
3633     (format "%2s-%s"
3634             (or (aref datevec 2) "??")
3635             (capitalize
3636              (or (car
3637                   (nth (1- (string-to-number (aref datevec 1)))
3638                        timezone-months-assoc))
3639                  "???")))))
3640
3641 ;; Make a hash table (default and minimum size is 255).
3642 ;; Optional argument HASHSIZE specifies the table size.
3643 (defun gnus-make-hashtable (&optional hashsize)
3644   (make-vector (if hashsize (max (gnus-create-hash-size hashsize) 255) 255) 0))
3645
3646 ;; Make a number that is suitable for hashing; bigger than MIN and one
3647 ;; less than 2^x.
3648 (defun gnus-create-hash-size (min)
3649   (let ((i 1))
3650     (while (< i min)
3651       (setq i (* 2 i)))
3652     (1- i)))
3653
3654 ;; Show message if message has a lower level than `gnus-verbose'.
3655 ;; Guideline for numbers:
3656 ;; 1 - error messages, 3 - non-serious error messages, 5 - messages
3657 ;; for things that take a long time, 7 - not very important messages
3658 ;; on stuff, 9 - messages inside loops.
3659 (defun gnus-message (level &rest args)
3660   (if (<= level gnus-verbose)
3661       (apply 'message args)
3662     ;; We have to do this format thingy here even if the result isn't
3663     ;; shown - the return value has to be the same as the return value
3664     ;; from `message'.
3665     (apply 'format args)))
3666
3667 (defun gnus-functionp (form)
3668   "Return non-nil if FORM is funcallable."
3669   (or (and (symbolp form) (fboundp form))
3670       (and (listp form) (eq (car form) 'lambda))))
3671
3672 ;; Generate a unique new group name.
3673 (defun gnus-generate-new-group-name (leaf)
3674   (let ((name leaf)
3675         (num 0))
3676     (while (gnus-gethash name gnus-newsrc-hashtb)
3677       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
3678     name))
3679
3680 ;; Find out whether the gnus-visual TYPE is wanted.
3681 (defun gnus-visual-p (&optional type class)
3682   (and gnus-visual                      ; Has to be non-nil, at least.
3683        (if (not type)                   ; We don't care about type.
3684            gnus-visual
3685          (if (listp gnus-visual)        ; It's a list, so we check it.
3686              (or (memq type gnus-visual)
3687                  (memq class gnus-visual))
3688            t))))
3689
3690 (defun gnus-parent-id (references)
3691   "Return the last Message-ID in REFERENCES."
3692   (when (and references
3693              (string-match "\\(<[^<>]+>\\)[ \t\n]*\\'" references))
3694     (substring references (match-beginning 1) (match-end 1))))
3695
3696 (defun gnus-split-references (references)
3697   "Return a list of Message-IDs in REFERENCES."
3698   (let ((beg 0)
3699         ids)
3700     (while (string-match "<[^>]+>" references beg)
3701       (push (substring references (match-beginning 0) (setq beg (match-end 0)))
3702             ids))
3703     (nreverse ids)))
3704
3705 (defun gnus-ephemeral-group-p (group)
3706   "Say whether GROUP is ephemeral or not."
3707   (gnus-group-get-parameter group 'quit-config))
3708
3709 (defun gnus-group-quit-config (group)
3710   "Return the quit-config of GROUP."
3711   (gnus-group-get-parameter group 'quit-config))
3712
3713 (defun gnus-simplify-mode-line ()
3714   "Make mode lines a bit simpler."
3715   (setq mode-line-modified "-- ")
3716   (when (listp mode-line-format)
3717     (make-local-variable 'mode-line-format)
3718     (setq mode-line-format (copy-sequence mode-line-format))
3719     (when (equal (nth 3 mode-line-format) "   ")
3720       (setcar (nthcdr 3 mode-line-format) " "))))
3721
3722 ;;; List and range functions
3723
3724 (defun gnus-last-element (list)
3725   "Return last element of LIST."
3726   (while (cdr list)
3727     (setq list (cdr list)))
3728   (car list))
3729
3730 (defun gnus-copy-sequence (list)
3731   "Do a complete, total copy of a list."
3732   (if (and (consp list) (not (consp (cdr list))))
3733       (cons (car list) (cdr list))
3734     (mapcar (lambda (elem) (if (consp elem)
3735                                (if (consp (cdr elem))
3736                                    (gnus-copy-sequence elem)
3737                                  (cons (car elem) (cdr elem)))
3738                              elem))
3739             list)))
3740
3741 (defun gnus-set-difference (list1 list2)
3742   "Return a list of elements of LIST1 that do not appear in LIST2."
3743   (let ((list1 (copy-sequence list1)))
3744     (while list2
3745       (setq list1 (delq (car list2) list1))
3746       (setq list2 (cdr list2)))
3747     list1))
3748
3749 (defun gnus-sorted-complement (list1 list2)
3750   "Return a list of elements of LIST1 that do not appear in LIST2.
3751 Both lists have to be sorted over <."
3752   (let (out)
3753     (if (or (null list1) (null list2))
3754         (or list1 list2)
3755       (while (and list1 list2)
3756         (cond ((= (car list1) (car list2))
3757                (setq list1 (cdr list1)
3758                      list2 (cdr list2)))
3759               ((< (car list1) (car list2))
3760                (setq out (cons (car list1) out))
3761                (setq list1 (cdr list1)))
3762               (t
3763                (setq out (cons (car list2) out))
3764                (setq list2 (cdr list2)))))
3765       (nconc (nreverse out) (or list1 list2)))))
3766
3767 (defun gnus-intersection (list1 list2)
3768   (let ((result nil))
3769     (while list2
3770       (if (memq (car list2) list1)
3771           (setq result (cons (car list2) result)))
3772       (setq list2 (cdr list2)))
3773     result))
3774
3775 (defun gnus-sorted-intersection (list1 list2)
3776   ;; LIST1 and LIST2 have to be sorted over <.
3777   (let (out)
3778     (while (and list1 list2)
3779       (cond ((= (car list1) (car list2))
3780              (setq out (cons (car list1) out)
3781                    list1 (cdr list1)
3782                    list2 (cdr list2)))
3783             ((< (car list1) (car list2))
3784              (setq list1 (cdr list1)))
3785             (t
3786              (setq list2 (cdr list2)))))
3787     (nreverse out)))
3788
3789 (defun gnus-set-sorted-intersection (list1 list2)
3790   ;; LIST1 and LIST2 have to be sorted over <.
3791   ;; This function modifies LIST1.
3792   (let* ((top (cons nil list1))
3793          (prev top))
3794     (while (and list1 list2)
3795       (cond ((= (car list1) (car list2))
3796              (setq prev list1
3797                    list1 (cdr list1)
3798                    list2 (cdr list2)))
3799             ((< (car list1) (car list2))
3800              (setcdr prev (cdr list1))
3801              (setq list1 (cdr list1)))
3802             (t
3803              (setq list2 (cdr list2)))))
3804     (setcdr prev nil)
3805     (cdr top)))
3806
3807 (defun gnus-compress-sequence (numbers &optional always-list)
3808   "Convert list of numbers to a list of ranges or a single range.
3809 If ALWAYS-LIST is non-nil, this function will always release a list of
3810 ranges."
3811   (let* ((first (car numbers))
3812          (last (car numbers))
3813          result)
3814     (if (null numbers)
3815         nil
3816       (if (not (listp (cdr numbers)))
3817           numbers
3818         (while numbers
3819           (cond ((= last (car numbers)) nil) ;Omit duplicated number
3820                 ((= (1+ last) (car numbers)) ;Still in sequence
3821                  (setq last (car numbers)))
3822                 (t                      ;End of one sequence
3823                  (setq result
3824                        (cons (if (= first last) first
3825                                (cons first last)) result))
3826                  (setq first (car numbers))
3827                  (setq last  (car numbers))))
3828           (setq numbers (cdr numbers)))
3829         (if (and (not always-list) (null result))
3830             (if (= first last) (list first) (cons first last))
3831           (nreverse (cons (if (= first last) first (cons first last))
3832                           result)))))))
3833
3834 (defalias 'gnus-uncompress-sequence 'gnus-uncompress-range)
3835 (defun gnus-uncompress-range (ranges)
3836   "Expand a list of ranges into a list of numbers.
3837 RANGES is either a single range on the form `(num . num)' or a list of
3838 these ranges."
3839   (let (first last result)
3840     (cond
3841      ((null ranges)
3842       nil)
3843      ((not (listp (cdr ranges)))
3844       (setq first (car ranges))
3845       (setq last (cdr ranges))
3846       (while (<= first last)
3847         (setq result (cons first result))
3848         (setq first (1+ first)))
3849       (nreverse result))
3850      (t
3851       (while ranges
3852         (if (atom (car ranges))
3853             (if (numberp (car ranges))
3854                 (setq result (cons (car ranges) result)))
3855           (setq first (caar ranges))
3856           (setq last  (cdar ranges))
3857           (while (<= first last)
3858             (setq result (cons first result))
3859             (setq first (1+ first))))
3860         (setq ranges (cdr ranges)))
3861       (nreverse result)))))
3862
3863 (defun gnus-add-to-range (ranges list)
3864   "Return a list of ranges that has all articles from both RANGES and LIST.
3865 Note: LIST has to be sorted over `<'."
3866   (if (not ranges)
3867       (gnus-compress-sequence list t)
3868     (setq list (copy-sequence list))
3869     (or (listp (cdr ranges))
3870         (setq ranges (list ranges)))
3871     (let ((out ranges)
3872           ilist lowest highest temp)
3873       (while (and ranges list)
3874         (setq ilist list)
3875         (setq lowest (or (and (atom (car ranges)) (car ranges))
3876                          (caar ranges)))
3877         (while (and list (cdr list) (< (cadr list) lowest))
3878           (setq list (cdr list)))
3879         (if (< (car ilist) lowest)
3880             (progn
3881               (setq temp list)
3882               (setq list (cdr list))
3883               (setcdr temp nil)
3884               (setq out (nconc (gnus-compress-sequence ilist t) out))))
3885         (setq highest (or (and (atom (car ranges)) (car ranges))
3886                           (cdar ranges)))
3887         (while (and list (<= (car list) highest))
3888           (setq list (cdr list)))
3889         (setq ranges (cdr ranges)))
3890       (if list
3891           (setq out (nconc (gnus-compress-sequence list t) out)))
3892       (setq out (sort out (lambda (r1 r2)
3893                             (< (or (and (atom r1) r1) (car r1))
3894                                (or (and (atom r2) r2) (car r2))))))
3895       (setq ranges out)
3896       (while ranges
3897         (if (atom (car ranges))
3898             (if (cdr ranges)
3899                 (if (atom (cadr ranges))
3900                     (if (= (1+ (car ranges)) (cadr ranges))
3901                         (progn
3902                           (setcar ranges (cons (car ranges)
3903                                                (cadr ranges)))
3904                           (setcdr ranges (cddr ranges))))
3905                   (if (= (1+ (car ranges)) (caadr ranges))
3906                       (progn
3907                         (setcar (cadr ranges) (car ranges))
3908                         (setcar ranges (cadr ranges))
3909                         (setcdr ranges (cddr ranges))))))
3910           (if (cdr ranges)
3911               (if (atom (cadr ranges))
3912                   (if (= (1+ (cdar ranges)) (cadr ranges))
3913                       (progn
3914                         (setcdr (car ranges) (cadr ranges))
3915                         (setcdr ranges (cddr ranges))))
3916                 (if (= (1+ (cdar ranges)) (caadr ranges))
3917                     (progn
3918                       (setcdr (car ranges) (cdadr ranges))
3919                       (setcdr ranges (cddr ranges)))))))
3920         (setq ranges (cdr ranges)))
3921       out)))
3922
3923 (defun gnus-remove-from-range (ranges list)
3924   "Return a list of ranges that has all articles from LIST removed from RANGES.
3925 Note: LIST has to be sorted over `<'."
3926   ;; !!! This function shouldn't look like this, but I've got a headache.
3927   (gnus-compress-sequence
3928    (gnus-sorted-complement
3929     (gnus-uncompress-range ranges) list)))
3930
3931 (defun gnus-member-of-range (number ranges)
3932   (if (not (listp (cdr ranges)))
3933       (and (>= number (car ranges))
3934            (<= number (cdr ranges)))
3935     (let ((not-stop t))
3936       (while (and ranges
3937                   (if (numberp (car ranges))
3938                       (>= number (car ranges))
3939                     (>= number (caar ranges)))
3940                   not-stop)
3941         (if (if (numberp (car ranges))
3942                 (= number (car ranges))
3943               (and (>= number (caar ranges))
3944                    (<= number (cdar ranges))))
3945             (setq not-stop nil))
3946         (setq ranges (cdr ranges)))
3947       (not not-stop))))
3948
3949 (defun gnus-range-length (range)
3950   "Return the length RANGE would have if uncompressed."
3951   (length (gnus-uncompress-range range)))
3952
3953 (defun gnus-sublist-p (list sublist)
3954   "Test whether all elements in SUBLIST are members of LIST."
3955   (let ((sublistp t))
3956     (while sublist
3957       (unless (memq (pop sublist) list)
3958         (setq sublistp nil
3959               sublist nil)))
3960     sublistp))
3961
3962 \f
3963 ;;;
3964 ;;; Gnus group mode
3965 ;;;
3966
3967 (defvar gnus-group-mode-map nil)
3968 (put 'gnus-group-mode 'mode-class 'special)
3969
3970 (unless gnus-group-mode-map
3971   (setq gnus-group-mode-map (make-keymap))
3972   (suppress-keymap gnus-group-mode-map)
3973
3974   (gnus-define-keys gnus-group-mode-map
3975     " " gnus-group-read-group
3976     "=" gnus-group-select-group
3977     "\M- " gnus-group-unhidden-select-group
3978     "\r" gnus-group-select-group
3979     "\M-\r" gnus-group-quick-select-group
3980     "j" gnus-group-jump-to-group
3981     "n" gnus-group-next-unread-group
3982     "p" gnus-group-prev-unread-group
3983     "\177" gnus-group-prev-unread-group
3984     [delete] gnus-group-prev-unread-group
3985     "N" gnus-group-next-group
3986     "P" gnus-group-prev-group
3987     "\M-n" gnus-group-next-unread-group-same-level
3988     "\M-p" gnus-group-prev-unread-group-same-level
3989     "," gnus-group-best-unread-group
3990     "." gnus-group-first-unread-group
3991     "u" gnus-group-unsubscribe-current-group
3992     "U" gnus-group-unsubscribe-group
3993     "c" gnus-group-catchup-current
3994     "C" gnus-group-catchup-current-all
3995     "l" gnus-group-list-groups
3996     "L" gnus-group-list-all-groups
3997     "m" gnus-group-mail
3998     "g" gnus-group-get-new-news
3999     "\M-g" gnus-group-get-new-news-this-group
4000     "R" gnus-group-restart
4001     "r" gnus-group-read-init-file
4002     "B" gnus-group-browse-foreign-server
4003     "b" gnus-group-check-bogus-groups
4004     "F" gnus-find-new-newsgroups
4005     "\C-c\C-d" gnus-group-describe-group
4006     "\M-d" gnus-group-describe-all-groups
4007     "\C-c\C-a" gnus-group-apropos
4008     "\C-c\M-\C-a" gnus-group-description-apropos
4009     "a" gnus-group-post-news
4010     "\ek" gnus-group-edit-local-kill
4011     "\eK" gnus-group-edit-global-kill
4012     "\C-k" gnus-group-kill-group
4013     "\C-y" gnus-group-yank-group
4014     "\C-w" gnus-group-kill-region
4015     "\C-x\C-t" gnus-group-transpose-groups
4016     "\C-c\C-l" gnus-group-list-killed
4017     "\C-c\C-x" gnus-group-expire-articles
4018     "\C-c\M-\C-x" gnus-group-expire-all-groups
4019     "V" gnus-version
4020     "s" gnus-group-save-newsrc
4021     "z" gnus-group-suspend
4022     "Z" gnus-group-clear-dribble
4023     "q" gnus-group-exit
4024     "Q" gnus-group-quit
4025     "?" gnus-group-describe-briefly
4026     "\C-c\C-i" gnus-info-find-node
4027     "\M-e" gnus-group-edit-group-method
4028     "^" gnus-group-enter-server-mode
4029     gnus-mouse-2 gnus-mouse-pick-group
4030     "<" beginning-of-buffer
4031     ">" end-of-buffer
4032     "\C-c\C-b" gnus-bug
4033     "\C-c\C-s" gnus-group-sort-groups
4034     "t" gnus-topic-mode
4035     "\C-c\M-g" gnus-activate-all-groups
4036     "\M-&" gnus-group-universal-argument
4037     "#" gnus-group-mark-group
4038     "\M-#" gnus-group-unmark-group)
4039
4040   (gnus-define-keys (gnus-group-mark-map "M" gnus-group-mode-map)
4041     "m" gnus-group-mark-group
4042     "u" gnus-group-unmark-group
4043     "w" gnus-group-mark-region
4044     "m" gnus-group-mark-buffer
4045     "r" gnus-group-mark-regexp
4046     "U" gnus-group-unmark-all-groups)
4047
4048   (gnus-define-keys (gnus-group-group-map "G" gnus-group-mode-map)
4049     "d" gnus-group-make-directory-group
4050     "h" gnus-group-make-help-group
4051     "a" gnus-group-make-archive-group
4052     "k" gnus-group-make-kiboze-group
4053     "m" gnus-group-make-group
4054     "E" gnus-group-edit-group
4055     "e" gnus-group-edit-group-method
4056     "p" gnus-group-edit-group-parameters
4057     "v" gnus-group-add-to-virtual
4058     "V" gnus-group-make-empty-virtual
4059     "D" gnus-group-enter-directory
4060     "f" gnus-group-make-doc-group
4061     "r" gnus-group-rename-group
4062     "\177" gnus-group-delete-group
4063     [delete] gnus-group-delete-group)
4064
4065    (gnus-define-keys (gnus-group-soup-map "s" gnus-group-group-map)
4066      "b" gnus-group-brew-soup
4067      "w" gnus-soup-save-areas
4068      "s" gnus-soup-send-replies
4069      "p" gnus-soup-pack-packet
4070      "r" nnsoup-pack-replies)
4071
4072    (gnus-define-keys (gnus-group-sort-map "S" gnus-group-group-map)
4073      "s" gnus-group-sort-groups
4074      "a" gnus-group-sort-groups-by-alphabet
4075      "u" gnus-group-sort-groups-by-unread
4076      "l" gnus-group-sort-groups-by-level
4077      "v" gnus-group-sort-groups-by-score
4078      "r" gnus-group-sort-groups-by-rank
4079      "m" gnus-group-sort-groups-by-method)
4080
4081    (gnus-define-keys (gnus-group-list-map "A" gnus-group-mode-map)
4082      "k" gnus-group-list-killed
4083      "z" gnus-group-list-zombies
4084      "s" gnus-group-list-groups
4085      "u" gnus-group-list-all-groups
4086      "A" gnus-group-list-active
4087      "a" gnus-group-apropos
4088      "d" gnus-group-description-apropos
4089      "m" gnus-group-list-matching
4090      "M" gnus-group-list-all-matching
4091      "l" gnus-group-list-level)
4092
4093    (gnus-define-keys (gnus-group-score-map "W" gnus-group-mode-map)
4094      "f" gnus-score-flush-cache)
4095
4096    (gnus-define-keys (gnus-group-help-map "H" gnus-group-mode-map)
4097      "f" gnus-group-fetch-faq)
4098
4099    (gnus-define-keys (gnus-group-sub-map "S" gnus-group-mode-map)
4100      "l" gnus-group-set-current-level
4101      "t" gnus-group-unsubscribe-current-group
4102      "s" gnus-group-unsubscribe-group
4103      "k" gnus-group-kill-group
4104      "y" gnus-group-yank-group
4105      "w" gnus-group-kill-region
4106      "\C-k" gnus-group-kill-level
4107      "z" gnus-group-kill-all-zombies))
4108
4109 (defun gnus-group-mode ()
4110   "Major mode for reading news.
4111
4112 All normal editing commands are switched off.
4113 \\<gnus-group-mode-map>
4114 The group buffer lists (some of) the groups available.  For instance,
4115 `\\[gnus-group-list-groups]' will list all subscribed groups with unread articles, while `\\[gnus-group-list-zombies]'
4116 lists all zombie groups.
4117
4118 Groups that are displayed can be entered with `\\[gnus-group-read-group]'.  To subscribe
4119 to a group not displayed, type `\\[gnus-group-unsubscribe-group]'.
4120
4121 For more in-depth information on this mode, read the manual (`\\[gnus-info-find-node]').
4122
4123 The following commands are available:
4124
4125 \\{gnus-group-mode-map}"
4126   (interactive)
4127   (when (and menu-bar-mode
4128              (gnus-visual-p 'group-menu 'menu))
4129     (gnus-group-make-menu-bar))
4130   (kill-all-local-variables)
4131   (gnus-simplify-mode-line)
4132   (setq major-mode 'gnus-group-mode)
4133   (setq mode-name "Group")
4134   (gnus-group-set-mode-line)
4135   (setq mode-line-process nil)
4136   (use-local-map gnus-group-mode-map)
4137   (buffer-disable-undo (current-buffer))
4138   (setq truncate-lines t)
4139   (setq buffer-read-only t)
4140   (run-hooks 'gnus-group-mode-hook))
4141
4142 (defun gnus-mouse-pick-group (e)
4143   "Enter the group under the mouse pointer."
4144   (interactive "e")
4145   (mouse-set-point e)
4146   (gnus-group-read-group nil))
4147
4148 ;; Look at LEVEL and find out what the level is really supposed to be.
4149 ;; If LEVEL is non-nil, LEVEL will be returned, if not, what happens
4150 ;; will depend on whether `gnus-group-use-permanent-levels' is used.
4151 (defun gnus-group-default-level (&optional level number-or-nil)
4152   (cond
4153    (gnus-group-use-permanent-levels
4154     (setq gnus-group-default-list-level
4155           (or level gnus-group-default-list-level))
4156     (or gnus-group-default-list-level gnus-level-subscribed))
4157    (number-or-nil
4158     level)
4159    (t
4160     (or level gnus-group-default-list-level gnus-level-subscribed))))
4161
4162 ;;;###autoload
4163 (defun gnus-slave-no-server (&optional arg)
4164   "Read network news as a slave, without connecting to local server"
4165   (interactive "P")
4166   (gnus-no-server arg t))
4167
4168 ;;;###autoload
4169 (defun gnus-no-server (&optional arg slave)
4170   "Read network news.
4171 If ARG is a positive number, Gnus will use that as the
4172 startup level.  If ARG is nil, Gnus will be started at level 2.
4173 If ARG is non-nil and not a positive number, Gnus will
4174 prompt the user for the name of an NNTP server to use.
4175 As opposed to `gnus', this command will not connect to the local server."
4176   (interactive "P")
4177   (make-local-variable 'gnus-group-use-permanent-levels)
4178   (setq gnus-group-use-permanent-levels t)
4179   (gnus (or arg (1- gnus-level-default-subscribed)) t slave))
4180
4181 ;;;###autoload
4182 (defun gnus-slave (&optional arg)
4183   "Read news as a slave."
4184   (interactive "P")
4185   (gnus arg nil 'slave))
4186
4187 ;;;###autoload
4188 (defun gnus-other-frame (&optional arg)
4189   "Pop up a frame to read news."
4190   (interactive "P")
4191   (if (get-buffer gnus-group-buffer)
4192       (let ((pop-up-frames t))
4193         (gnus arg))
4194     (select-frame (make-frame))
4195     (gnus arg)))
4196
4197 ;;;###autoload
4198 (defun gnus (&optional arg dont-connect slave)
4199   "Read network news.
4200 If ARG is non-nil and a positive number, Gnus will use that as the
4201 startup level.  If ARG is non-nil and not a positive number, Gnus will
4202 prompt the user for the name of an NNTP server to use."
4203   (interactive "P")
4204
4205   (if (get-buffer gnus-group-buffer)
4206       (progn
4207         (switch-to-buffer gnus-group-buffer)
4208         (gnus-group-get-new-news))
4209
4210     (gnus-clear-system)
4211     (nnheader-init-server-buffer)
4212     (gnus-read-init-file)
4213     (setq gnus-slave slave)
4214
4215     (gnus-group-setup-buffer)
4216     (let ((buffer-read-only nil))
4217       (erase-buffer)
4218       (if (not gnus-inhibit-startup-message)
4219           (progn
4220             (gnus-group-startup-message)
4221             (sit-for 0))))
4222
4223     (let ((level (and (numberp arg) (> arg 0) arg))
4224           did-connect)
4225       (unwind-protect
4226           (progn
4227             (or dont-connect
4228                 (setq did-connect
4229                       (gnus-start-news-server (and arg (not level))))))
4230         (if (and (not dont-connect)
4231                  (not did-connect))
4232             (gnus-group-quit)
4233           (run-hooks 'gnus-startup-hook)
4234           ;; NNTP server is successfully open.
4235
4236           ;; Find the current startup file name.
4237           (setq gnus-current-startup-file
4238                 (gnus-make-newsrc-file gnus-startup-file))
4239
4240           ;; Read the dribble file.
4241           (and (or gnus-slave gnus-use-dribble-file) (gnus-dribble-read-file))
4242
4243           ;; Allow using GroupLens predictions.
4244           (when gnus-use-grouplens
4245             (bbb-login)
4246             (add-hook 'gnus-summary-mode-hook 'gnus-grouplens-mode))
4247
4248           (gnus-summary-make-display-table)
4249           ;; Do the actual startup.
4250           (gnus-setup-news nil level)
4251           ;; Generate the group buffer.
4252           (gnus-group-list-groups level)
4253           (gnus-group-first-unread-group)
4254           (gnus-configure-windows 'group)
4255           (gnus-group-set-mode-line))))))
4256
4257 (defun gnus-unload ()
4258   "Unload all Gnus features."
4259   (interactive)
4260   (or (boundp 'load-history)
4261       (error "Sorry, `gnus-unload' is not implemented in this Emacs version."))
4262   (let ((history load-history)
4263         feature)
4264     (while history
4265       (and (string-match "^\\(gnus\\|nn\\)" (caar history))
4266            (setq feature (cdr (assq 'provide (car history))))
4267            (unload-feature feature 'force))
4268       (setq history (cdr history)))))
4269
4270 (defun gnus-compile ()
4271   "Byte-compile the user-defined format specs."
4272   (interactive)
4273   (let ((entries gnus-format-specs)
4274         entry gnus-tmp-func)
4275     (save-excursion
4276       (gnus-message 7 "Compiling format specs...")
4277
4278       (while entries
4279         (setq entry (pop entries))
4280         (if (eq (car entry) 'version)
4281             (setq gnus-format-specs (delq entry gnus-format-specs))
4282           (when (and (listp (caddr entry))
4283                      (not (eq 'byte-code (caaddr entry))))
4284             (fset 'gnus-tmp-func
4285                   `(lambda () ,(caddr entry)))
4286             (byte-compile 'gnus-tmp-func)
4287             (setcar (cddr entry) (gnus-byte-code 'gnus-tmp-func)))))
4288
4289       (push (cons 'version emacs-version) gnus-format-specs)
4290
4291       (gnus-message 7 "Compiling user specs...done"))))
4292
4293 (defun gnus-indent-rigidly (start end arg)
4294   "Indent rigidly using only spaces and no tabs."
4295   (save-excursion
4296     (save-restriction
4297       (narrow-to-region start end)
4298       (indent-rigidly start end arg)
4299       (goto-char (point-min))
4300       (while (search-forward "\t" nil t)
4301         (replace-match "        " t t)))))
4302
4303 (defun gnus-group-startup-message (&optional x y)
4304   "Insert startup message in current buffer."
4305   ;; Insert the message.
4306   (erase-buffer)
4307   (insert
4308    (format "              %s
4309           _    ___ _             _
4310           _ ___ __ ___  __    _ ___
4311           __   _     ___    __  ___
4312               _           ___     _
4313              _  _ __             _
4314              ___   __            _
4315                    __           _
4316                     _      _   _
4317                    _      _    _
4318                       _  _    _
4319                   __  ___
4320                  _   _ _     _
4321                 _   _
4322               _    _
4323              _    _
4324             _
4325           __
4326
4327 "
4328            ""))
4329   ;; And then hack it.
4330   (gnus-indent-rigidly (point-min) (point-max)
4331                        (/ (max (- (window-width) (or x 46)) 0) 2))
4332   (goto-char (point-min))
4333   (forward-line 1)
4334   (let* ((pheight (count-lines (point-min) (point-max)))
4335          (wheight (window-height))
4336          (rest (- wheight pheight)))
4337     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
4338   ;; Fontify some.
4339   (goto-char (point-min))
4340   (and (search-forward "Praxis" nil t)
4341        (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
4342   (goto-char (point-min))
4343   (let* ((mode-string (gnus-group-set-mode-line)))
4344     (setq mode-line-buffer-identification
4345           (list (concat gnus-version (substring (car mode-string) 4))))
4346     (set-buffer-modified-p t)))
4347
4348 (defun gnus-group-setup-buffer ()
4349   (or (get-buffer gnus-group-buffer)
4350       (progn
4351         (switch-to-buffer gnus-group-buffer)
4352         (gnus-add-current-to-buffer-list)
4353         (gnus-group-mode)
4354         (and gnus-carpal (gnus-carpal-setup-buffer 'group)))))
4355
4356 (defun gnus-group-list-groups (&optional level unread lowest)
4357   "List newsgroups with level LEVEL or lower that have unread articles.
4358 Default is all subscribed groups.
4359 If argument UNREAD is non-nil, groups with no unread articles are also
4360 listed."
4361   (interactive (list (if current-prefix-arg
4362                          (prefix-numeric-value current-prefix-arg)
4363                        (or
4364                         (gnus-group-default-level nil t)
4365                         gnus-group-default-list-level
4366                         gnus-level-subscribed))))
4367   (or level
4368       (setq level (car gnus-group-list-mode)
4369             unread (cdr gnus-group-list-mode)))
4370   (setq level (gnus-group-default-level level))
4371   (gnus-group-setup-buffer)             ;May call from out of group buffer
4372   (gnus-update-format-specifications)
4373   (let ((case-fold-search nil)
4374         (props (text-properties-at (gnus-point-at-bol)))
4375         (group (gnus-group-group-name)))
4376     (funcall gnus-group-prepare-function level unread lowest)
4377     (if (zerop (buffer-size))
4378         (gnus-message 5 gnus-no-groups-message)
4379       (goto-char (point-max))
4380       (when (or (not gnus-group-goto-next-group-function)
4381                 (not (funcall gnus-group-goto-next-group-function 
4382                               group props)))
4383         (if (not group)
4384             ;; Go to the first group with unread articles.
4385             (gnus-group-search-forward t)
4386           ;; Find the right group to put point on.  If the current group
4387           ;; has disappeared in the new listing, try to find the next
4388           ;; one.        If no next one can be found, just leave point at the
4389           ;; first newsgroup in the buffer.
4390           (if (not (gnus-goto-char
4391                     (text-property-any
4392                      (point-min) (point-max)
4393                      'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
4394               (let ((newsrc (cdddr (gnus-gethash group gnus-newsrc-hashtb))))
4395                 (while (and newsrc
4396                             (not (gnus-goto-char
4397                                   (text-property-any
4398                                    (point-min) (point-max) 'gnus-group
4399                                    (gnus-intern-safe
4400                                     (caar newsrc) gnus-active-hashtb)))))
4401                   (setq newsrc (cdr newsrc)))
4402                 (or newsrc (progn (goto-char (point-max))
4403                                   (forward-line -1)))))))
4404       ;; Adjust cursor point.
4405       (gnus-group-position-point))))
4406
4407 (defun gnus-group-list-level (level &optional all)
4408   "List groups on LEVEL.
4409 If ALL (the prefix), also list groups that have no unread articles."
4410   (interactive "nList groups on level: \nP")
4411   (gnus-group-list-groups level all level))
4412
4413 (defun gnus-group-prepare-flat (level &optional all lowest regexp)
4414   "List all newsgroups with unread articles of level LEVEL or lower.
4415 If ALL is non-nil, list groups that have no unread articles.
4416 If LOWEST is non-nil, list all newsgroups of level LOWEST or higher.
4417 If REGEXP, only list groups matching REGEXP."
4418   (set-buffer gnus-group-buffer)
4419   (let ((buffer-read-only nil)
4420         (newsrc (cdr gnus-newsrc-alist))
4421         (lowest (or lowest 1))
4422         info clevel unread group params)
4423     (erase-buffer)
4424     (if (< lowest gnus-level-zombie)
4425         ;; List living groups.
4426         (while newsrc
4427           (setq info (car newsrc)
4428                 group (gnus-info-group info)
4429                 params (gnus-info-params info)
4430                 newsrc (cdr newsrc)
4431                 unread (car (gnus-gethash group gnus-newsrc-hashtb)))
4432           (and unread                   ; This group might be bogus
4433                (or (not regexp)
4434                    (string-match regexp group))
4435                (<= (setq clevel (gnus-info-level info)) level)
4436                (>= clevel lowest)
4437                (or all                  ; We list all groups?
4438                    (and gnus-group-list-inactive-groups
4439                         (eq unread t))  ; We list unactivated groups
4440                    (> unread 0)         ; We list groups with unread articles
4441                    (and gnus-list-groups-with-ticked-articles
4442                         (cdr (assq 'tick (gnus-info-marks info))))
4443                                         ; And groups with tickeds
4444                    ;; Check for permanent visibility.
4445                    (and gnus-permanently-visible-groups
4446                         (string-match gnus-permanently-visible-groups
4447                                       group))
4448                    (memq 'visible params)
4449                    (cdr (assq 'visible params)))
4450                (gnus-group-insert-group-line
4451                 group (gnus-info-level info)
4452                 (gnus-info-marks info) unread (gnus-info-method info)))))
4453
4454     ;; List dead groups.
4455     (and (>= level gnus-level-zombie) (<= lowest gnus-level-zombie)
4456          (gnus-group-prepare-flat-list-dead
4457           (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
4458           gnus-level-zombie ?Z
4459           regexp))
4460     (and (>= level gnus-level-killed) (<= lowest gnus-level-killed)
4461          (gnus-group-prepare-flat-list-dead
4462           (setq gnus-killed-list (sort gnus-killed-list 'string<))
4463           gnus-level-killed ?K regexp))
4464
4465     (gnus-group-set-mode-line)
4466     (setq gnus-group-list-mode (cons level all))
4467     (run-hooks 'gnus-group-prepare-hook)))
4468
4469 (defun gnus-group-prepare-flat-list-dead (groups level mark regexp)
4470   ;; List zombies and killed lists somewhat faster, which was
4471   ;; suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.  It does
4472   ;; this by ignoring the group format specification altogether.
4473   (let (group beg)
4474     (if regexp
4475         ;; This loop is used when listing groups that match some
4476         ;; regexp.
4477         (while groups
4478           (setq group (pop groups))
4479           (when (string-match regexp group)
4480             (add-text-properties
4481              (point) (prog1 (1+ (point))
4482                        (insert " " mark "     *: " group "\n"))
4483              (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4484                    'gnus-unread t
4485                    'gnus-level level))))
4486       ;; This loop is used when listing all groups.
4487       (while groups
4488         (add-text-properties
4489          (point) (prog1 (1+ (point))
4490                    (insert " " mark "     *: "
4491                            (setq group (pop groups)) "\n"))
4492          (list 'gnus-group (gnus-intern-safe group gnus-active-hashtb)
4493                'gnus-unread t
4494                'gnus-level level))))))
4495
4496 (defmacro gnus-group-real-name (group)
4497   "Find the real name of a foreign newsgroup."
4498   `(let ((gname ,group))
4499      (if (string-match ":[^:]+$" gname)
4500          (substring gname (1+ (match-beginning 0)))
4501        gname)))
4502
4503 (defsubst gnus-server-add-address (method)
4504   (let ((method-name (symbol-name (car method))))
4505     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
4506              (not (assq (intern (concat method-name "-address")) method)))
4507         (append method (list (list (intern (concat method-name "-address"))
4508                                    (nth 1 method))))
4509       method)))
4510
4511 (defsubst gnus-server-get-method (group method)
4512   ;; Input either a server name, and extended server name, or a
4513   ;; select method, and return a select method.
4514   (cond ((stringp method)
4515          (gnus-server-to-method method))
4516         ((and (stringp (car method)) group)
4517          (gnus-server-extend-method group method))
4518         (t
4519          (gnus-server-add-address method))))
4520
4521 (defun gnus-server-to-method (server)
4522   "Map virtual server names to select methods."
4523   (or 
4524    ;; Perhaps this is the native server?
4525    (and (equal server "native") gnus-select-method)
4526    ;; It should be in the server alist.
4527    (cdr (assoc server gnus-server-alist))
4528    ;; If not, we look through all the opened server
4529    ;; to see whether we can find it there.
4530    (let ((opened gnus-opened-servers))
4531      (while (and opened
4532                  (not (equal server (format "%s:%s" (caaar opened)
4533                                             (cadaar opened)))))
4534        (pop opened))
4535      (caar opened))))
4536
4537 (defmacro gnus-method-equal (ss1 ss2)
4538   "Say whether two servers are equal."
4539   `(let ((s1 ,ss1)
4540          (s2 ,ss2))
4541      (or (equal s1 s2)
4542          (and (= (length s1) (length s2))
4543               (progn
4544                 (while (and s1 (member (car s1) s2))
4545                   (setq s1 (cdr s1)))
4546                 (null s1))))))
4547
4548 (defun gnus-server-equal (m1 m2)
4549   "Say whether two methods are equal."
4550   (let ((m1 (cond ((null m1) gnus-select-method)
4551                   ((stringp m1) (gnus-server-to-method m1))
4552                   (t m1)))
4553         (m2 (cond ((null m2) gnus-select-method)
4554                   ((stringp m2) (gnus-server-to-method m2))
4555                   (t m2))))
4556     (gnus-method-equal m1 m2)))
4557
4558 (defun gnus-group-prefixed-name (group method)
4559   "Return the whole name from GROUP and METHOD."
4560   (and (stringp method) (setq method (gnus-server-to-method method)))
4561   (concat (format "%s" (car method))
4562           (if (and
4563                (or (assoc (format "%s" (car method)) 
4564                           (gnus-methods-using 'address))
4565                    (gnus-server-equal method gnus-message-archive-method))
4566                (nth 1 method)
4567                (not (string= (nth 1 method) "")))
4568               (concat "+" (nth 1 method)))
4569           ":" group))
4570
4571 (defun gnus-group-real-prefix (group)
4572   "Return the prefix of the current group name."
4573   (if (string-match "^[^:]+:" group)
4574       (substring group 0 (match-end 0))
4575     ""))
4576
4577 (defun gnus-group-method (group)
4578   "Return the server or method used for selecting GROUP."
4579   (let ((prefix (gnus-group-real-prefix group)))
4580     (if (equal prefix "")
4581         gnus-select-method
4582       (let ((servers gnus-opened-servers)
4583             (server "")
4584             backend possible found)
4585         (if (string-match "^[^\\+]+\\+" prefix)
4586             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
4587                   server (substring prefix (match-end 0) (1- (length prefix))))
4588           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
4589         (while servers
4590           (when (eq (caaar servers) backend)
4591             (setq possible (caar servers))
4592             (when (equal (cadaar servers) server)
4593               (setq found (caar servers))))
4594           (pop servers))
4595         (or (car (rassoc found gnus-server-alist))
4596             found
4597             (car (rassoc possible gnus-server-alist))
4598             possible
4599             (list backend server))))))
4600
4601 (defsubst gnus-secondary-method-p (method)
4602   "Return whether METHOD is a secondary select method."
4603   (let ((methods gnus-secondary-select-methods)
4604         (gmethod (gnus-server-get-method nil method)))
4605     (while (and methods
4606                 (not (equal (gnus-server-get-method nil (car methods))
4607                             gmethod)))
4608       (setq methods (cdr methods)))
4609     methods))
4610
4611 (defun gnus-group-foreign-p (group)
4612   "Say whether a group is foreign or not."
4613   (and (not (gnus-group-native-p group))
4614        (not (gnus-group-secondary-p group))))
4615
4616 (defun gnus-group-native-p (group)
4617   "Say whether the group is native or not."
4618   (not (string-match ":" group)))
4619
4620 (defun gnus-group-secondary-p (group)
4621   "Say whether the group is secondary or not."
4622   (gnus-secondary-method-p (gnus-find-method-for-group group)))
4623
4624 (defun gnus-group-get-parameter (group &optional symbol)
4625   "Returns the group parameters for GROUP.
4626 If SYMBOL, return the value of that symbol in the group parameters."
4627   (let ((params (gnus-info-params (gnus-get-info group))))
4628     (if symbol
4629         (gnus-group-parameter-value params symbol)
4630       params)))
4631
4632 (defun gnus-group-parameter-value (params symbol)
4633   "Return the value of SYMBOL in group PARAMS."
4634   (or (car (memq symbol params))        ; It's either a simple symbol
4635       (cdr (assq symbol params))))      ; or a cons.
4636
4637 (defun gnus-group-add-parameter (group param)
4638   "Add parameter PARAM to GROUP."
4639   (let ((info (gnus-get-info group)))
4640     (if (not info)
4641         () ; This is a dead group.  We just ignore it.
4642       ;; Cons the new param to the old one and update.
4643       (gnus-group-set-info (cons param (gnus-info-params info))
4644                            group 'params))))
4645
4646 (defun gnus-group-set-parameter (group name value)
4647   "Set parameter NAME to VALUE in GROUP."
4648   (let ((info (gnus-get-info group)))
4649     (if (not info)
4650         () ; This is a dead group.  We just ignore it.
4651       (let ((old-params (gnus-info-params info))
4652             (new-params (list (cons name value))))
4653         (while old-params
4654           (if (or (not (listp (car old-params)))
4655                   (not (eq (caar old-params) name)))
4656               (setq new-params (append new-params (list (car old-params)))))
4657           (setq old-params (cdr old-params)))
4658         (gnus-group-set-info new-params group 'params)))))
4659
4660 (defun gnus-group-add-score (group &optional score)
4661   "Add SCORE to the GROUP score.
4662 If SCORE is nil, add 1 to the score of GROUP."
4663   (let ((info (gnus-get-info group)))
4664     (gnus-info-set-score info (+ (gnus-info-score info) (or score 1)))))
4665
4666 (defun gnus-summary-bubble-group ()
4667   "Increase the score of the current group.
4668 This is a handy function to add to `gnus-summary-exit-hook' to
4669 increase the score of each group you read."
4670   (gnus-group-add-score gnus-newsgroup-name))
4671
4672 (defun gnus-group-set-info (info &optional method-only-group part)
4673   (let* ((entry (gnus-gethash
4674                  (or method-only-group (gnus-info-group info))
4675                  gnus-newsrc-hashtb))
4676          (part-info info)
4677          (info (if method-only-group (nth 2 entry) info))
4678          method)
4679     (when method-only-group
4680       (unless entry
4681         (error "Trying to change non-existent group %s" method-only-group))
4682       ;; We have received parts of the actual group info - either the
4683       ;; select method or the group parameters.  We first check
4684       ;; whether we have to extend the info, and if so, do that.
4685       (let ((len (length info))
4686             (total (if (eq part 'method) 5 6)))
4687         (when (< len total)
4688           (setcdr (nthcdr (1- len) info)
4689                   (make-list (- total len) nil)))
4690         ;; Then we enter the new info.
4691         (setcar (nthcdr (1- total) info) part-info)))
4692     (unless entry
4693       ;; This is a new group, so we just create it.
4694       (save-excursion
4695         (set-buffer gnus-group-buffer)
4696         (setq method (gnus-info-method info))
4697         (when (gnus-server-equal method "native")
4698           (setq method nil))
4699         (if method
4700             ;; It's a foreign group...
4701             (gnus-group-make-group
4702              (gnus-group-real-name (gnus-info-group info))
4703              (if (stringp method) method
4704                (prin1-to-string (car method)))
4705              (and (consp method)
4706                   (nth 1 (gnus-info-method info))))
4707           ;; It's a native group.
4708           (gnus-group-make-group (gnus-info-group info)))
4709         (gnus-message 6 "Note: New group created")
4710         (setq entry
4711               (gnus-gethash (gnus-group-prefixed-name
4712                              (gnus-group-real-name (gnus-info-group info))
4713                              (or (gnus-info-method info) gnus-select-method))
4714                             gnus-newsrc-hashtb))))
4715     ;; Whether it was a new group or not, we now have the entry, so we
4716     ;; can do the update.
4717     (if entry
4718         (progn
4719           (setcar (nthcdr 2 entry) info)
4720           (when (and (not (eq (car entry) t))
4721                      (gnus-active (gnus-info-group info)))
4722             (let ((marked (gnus-info-marks info)))
4723               (setcar entry (length (gnus-list-of-unread-articles
4724                                      (car info)))))))
4725       (error "No such group: %s" (gnus-info-group info)))))
4726
4727 (defun gnus-group-set-method-info (group select-method)
4728   (gnus-group-set-info select-method group 'method))
4729
4730 (defun gnus-group-set-params-info (group params)
4731   (gnus-group-set-info params group 'params))
4732
4733 (defun gnus-group-update-group-line ()
4734   "Update the current line in the group buffer."
4735   (let* ((buffer-read-only nil)
4736          (group (gnus-group-group-name))
4737          (gnus-group-indentation (gnus-group-group-indentation))
4738          (entry (and group (gnus-gethash group gnus-newsrc-hashtb))))
4739     (and entry
4740          (not (gnus-ephemeral-group-p group))
4741          (gnus-dribble-enter
4742           (concat "(gnus-group-set-info '"
4743                   (prin1-to-string (nth 2 entry)) ")")))
4744     (gnus-delete-line)
4745     (gnus-group-insert-group-line-info group)
4746     (forward-line -1)
4747     (gnus-group-position-point)))
4748
4749 (defun gnus-group-insert-group-line-info (group)
4750   "Insert GROUP on the current line."
4751   (let ((entry (gnus-gethash group gnus-newsrc-hashtb))
4752         active info)
4753     (if entry
4754         (progn
4755           ;; (Un)subscribed group.
4756           (setq info (nth 2 entry))
4757           (gnus-group-insert-group-line
4758            group (gnus-info-level info) (gnus-info-marks info)
4759            (or (car entry) t) (gnus-info-method info)))
4760       ;; This group is dead.
4761       (gnus-group-insert-group-line
4762        group
4763        (if (member group gnus-zombie-list) gnus-level-zombie gnus-level-killed)
4764        nil
4765        (if (setq active (gnus-active group))
4766            (- (1+ (cdr active)) (car active)) 0)
4767        nil))))
4768
4769 (defun gnus-group-insert-group-line
4770   (gnus-tmp-group gnus-tmp-level gnus-tmp-marked number
4771                   gnus-tmp-method)
4772   "Insert a group line in the group buffer."
4773   (let* ((gnus-tmp-active (gnus-active gnus-tmp-group))
4774          (gnus-tmp-number-total
4775           (if gnus-tmp-active
4776               (1+ (- (cdr gnus-tmp-active) (car gnus-tmp-active)))
4777             0))
4778          (gnus-tmp-number-of-unread
4779           (if (numberp number) (int-to-string (max 0 number))
4780             "*"))
4781          (gnus-tmp-number-of-read
4782           (if (numberp number)
4783               (int-to-string (max 0 (- gnus-tmp-number-total number)))
4784             "*"))
4785          (gnus-tmp-subscribed
4786           (cond ((<= gnus-tmp-level gnus-level-subscribed) ? )
4787                 ((<= gnus-tmp-level gnus-level-unsubscribed) ?U)
4788                 ((= gnus-tmp-level gnus-level-zombie) ?Z)
4789                 (t ?K)))
4790          (gnus-tmp-qualified-group (gnus-group-real-name gnus-tmp-group))
4791          (gnus-tmp-newsgroup-description
4792           (if gnus-description-hashtb
4793               (or (gnus-gethash gnus-tmp-group gnus-description-hashtb) "")
4794             ""))
4795          (gnus-tmp-moderated
4796           (if (member gnus-tmp-group gnus-moderated-list) ?m ? ))
4797          (gnus-tmp-moderated-string
4798           (if (eq gnus-tmp-moderated ?m) "(m)" ""))
4799          (gnus-tmp-method
4800           (gnus-server-get-method gnus-tmp-group gnus-tmp-method))
4801          (gnus-tmp-news-server (or (cadr gnus-tmp-method) ""))
4802          (gnus-tmp-news-method (or (car gnus-tmp-method) ""))
4803          (gnus-tmp-news-method-string
4804           (if gnus-tmp-method
4805               (format "(%s:%s)" (car gnus-tmp-method)
4806                       (cadr gnus-tmp-method)) ""))
4807          (gnus-tmp-marked-mark
4808           (if (and (numberp number)
4809                    (zerop number)
4810                    (cdr (assq 'tick gnus-tmp-marked)))
4811               ?* ? ))
4812          (gnus-tmp-process-marked
4813           (if (member gnus-tmp-group gnus-group-marked)
4814               gnus-process-mark ? ))
4815          (gnus-tmp-grouplens
4816           (or (and gnus-use-grouplens
4817                    (bbb-grouplens-group-p gnus-tmp-group))
4818               ""))
4819          (buffer-read-only nil)
4820          header gnus-tmp-header)        ; passed as parameter to user-funcs.
4821     (beginning-of-line)
4822     (add-text-properties
4823      (point)
4824      (prog1 (1+ (point))
4825        ;; Insert the text.
4826        (eval gnus-group-line-format-spec))
4827      `(gnus-group ,(gnus-intern-safe gnus-tmp-group gnus-active-hashtb)
4828        gnus-unread ,(if (numberp number)
4829                         (string-to-int gnus-tmp-number-of-unread)
4830                       t)
4831        gnus-marked ,gnus-tmp-marked-mark
4832        gnus-indentation ,gnus-group-indentation
4833        gnus-level ,gnus-tmp-level))
4834     (when (gnus-visual-p 'group-highlight 'highlight)
4835       (forward-line -1)
4836       (run-hooks 'gnus-group-update-hook)
4837       (forward-line))
4838     ;; Allow XEmacs to remove front-sticky text properties.
4839     (gnus-group-remove-excess-properties)))
4840
4841 (defun gnus-group-update-group (group &optional visible-only)
4842   "Update all lines where GROUP appear.
4843 If VISIBLE-ONLY is non-nil, the group won't be displayed if it isn't
4844 already."
4845   (save-excursion
4846     (set-buffer gnus-group-buffer)
4847     ;; The buffer may be narrowed.
4848     (save-restriction
4849       (widen)
4850       (let ((ident (gnus-intern-safe group gnus-active-hashtb))
4851             (loc (point-min))
4852             found buffer-read-only visible)
4853         ;; Enter the current status into the dribble buffer.
4854         (let ((entry (gnus-gethash group gnus-newsrc-hashtb)))
4855           (if (and entry (not (gnus-ephemeral-group-p group)))
4856               (gnus-dribble-enter
4857                (concat "(gnus-group-set-info '" (prin1-to-string (nth 2 entry))
4858                        ")"))))
4859         ;; Find all group instances.  If topics are in use, each group
4860         ;; may be listed in more than once.
4861         (while (setq loc (text-property-any
4862                           loc (point-max) 'gnus-group ident))
4863           (setq found t)
4864           (goto-char loc)
4865           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4866             (gnus-delete-line)
4867             (gnus-group-insert-group-line-info group))
4868           (setq loc (1+ loc)))
4869         (unless (or found visible-only)
4870           ;; No such line in the buffer, find out where it's supposed to
4871           ;; go, and insert it there (or at the end of the buffer).
4872           (if gnus-goto-missing-group-function
4873               (funcall gnus-goto-missing-group-function group)
4874             (let ((entry (cddr (gnus-gethash group gnus-newsrc-hashtb))))
4875               (while (and entry (car entry)
4876                           (not
4877                            (gnus-goto-char
4878                             (text-property-any
4879                              (point-min) (point-max)
4880                              'gnus-group (gnus-intern-safe
4881                                           (caar entry) gnus-active-hashtb)))))
4882                 (setq entry (cdr entry)))
4883               (or entry (goto-char (point-max)))))
4884           ;; Finally insert the line.
4885           (let ((gnus-group-indentation (gnus-group-group-indentation)))
4886             (gnus-group-insert-group-line-info group)))
4887         (gnus-group-set-mode-line)))))
4888
4889 (defun gnus-group-set-mode-line ()
4890   (when (memq 'group gnus-updated-mode-lines)
4891     (let* ((gformat (or gnus-group-mode-line-format-spec
4892                         (setq gnus-group-mode-line-format-spec
4893                               (gnus-parse-format
4894                                gnus-group-mode-line-format
4895                                gnus-group-mode-line-format-alist))))
4896            (gnus-tmp-news-server (cadr gnus-select-method))
4897            (gnus-tmp-news-method (car gnus-select-method))
4898            (max-len 60)
4899            gnus-tmp-header                      ;Dummy binding for user-defined formats
4900            ;; Get the resulting string.
4901            (mode-string (eval gformat)))
4902       ;; If the line is too long, we chop it off.
4903       (when (> (length mode-string) max-len)
4904         (setq mode-string (substring mode-string 0 (- max-len 4))))
4905       (prog1
4906           (setq mode-line-buffer-identification (list mode-string))
4907         (set-buffer-modified-p t)))))
4908
4909 (defun gnus-group-group-name ()
4910   "Get the name of the newsgroup on the current line."
4911   (let ((group (get-text-property (gnus-point-at-bol) 'gnus-group)))
4912     (and group (symbol-name group))))
4913
4914 (defun gnus-group-group-level ()
4915   "Get the level of the newsgroup on the current line."
4916   (get-text-property (gnus-point-at-bol) 'gnus-level))
4917
4918 (defun gnus-group-group-indentation ()
4919   "Get the indentation of the newsgroup on the current line."
4920   (or (get-text-property (gnus-point-at-bol) 'gnus-indentation) ""))
4921
4922 (defun gnus-group-group-unread ()
4923   "Get the number of unread articles of the newsgroup on the current line."
4924   (get-text-property (gnus-point-at-bol) 'gnus-unread))
4925
4926 (defun gnus-group-search-forward (&optional backward all level first-too)
4927   "Find the next newsgroup with unread articles.
4928 If BACKWARD is non-nil, find the previous newsgroup instead.
4929 If ALL is non-nil, just find any newsgroup.
4930 If LEVEL is non-nil, find group with level LEVEL, or higher if no such
4931 group exists.
4932 If FIRST-TOO, the current line is also eligible as a target."
4933   (let ((way (if backward -1 1))
4934         (low gnus-level-killed)
4935         (beg (point))
4936         pos found lev)
4937     (if (and backward (progn (beginning-of-line)) (bobp))
4938         nil
4939       (or first-too (forward-line way))
4940       (while (and
4941               (not (eobp))
4942               (not (setq
4943                     found
4944                     (and (or all
4945                              (and
4946                               (let ((unread
4947                                      (get-text-property (point) 'gnus-unread)))
4948                                 (and (numberp unread) (> unread 0)))
4949                               (setq lev (get-text-property (point)
4950                                                            'gnus-level))
4951                               (<= lev gnus-level-subscribed)))
4952                          (or (not level)
4953                              (and (setq lev (get-text-property (point)
4954                                                                'gnus-level))
4955                                   (or (= lev level)
4956                                       (and (< lev low)
4957                                            (< level lev)
4958                                            (progn
4959                                              (setq low lev)
4960                                              (setq pos (point))
4961                                              nil))))))))
4962               (zerop (forward-line way)))))
4963     (if found
4964         (progn (gnus-group-position-point) t)
4965       (goto-char (or pos beg))
4966       (and pos t))))
4967
4968 ;;; Gnus group mode commands
4969
4970 ;; Group marking.
4971
4972 (defun gnus-group-mark-group (n &optional unmark no-advance)
4973   "Mark the current group."
4974   (interactive "p")
4975   (let ((buffer-read-only nil)
4976         group)
4977     (while
4978         (and (> n 0)
4979              (setq group (gnus-group-group-name))
4980              (progn
4981                (beginning-of-line)
4982                (forward-char
4983                 (or (cdr (assq 'process gnus-group-mark-positions)) 2))
4984                (delete-char 1)
4985                (if unmark
4986                    (progn
4987                      (insert " ")
4988                      (setq gnus-group-marked (delete group gnus-group-marked)))
4989                  (insert "#")
4990                  (setq gnus-group-marked
4991                        (cons group (delete group gnus-group-marked))))
4992                t)
4993              (or no-advance (zerop (gnus-group-next-group 1))))
4994       (setq n (1- n)))
4995     (gnus-summary-position-point)
4996     n))
4997
4998 (defun gnus-group-unmark-group (n)
4999   "Remove the mark from the current group."
5000   (interactive "p")
5001   (gnus-group-mark-group n 'unmark)
5002   (gnus-group-position-point))
5003
5004 (defun gnus-group-unmark-all-groups ()
5005   "Unmark all groups."
5006   (let ((groups gnus-group-marked))
5007     (save-excursion
5008       (while groups
5009         (gnus-group-remove-mark (pop groups)))))
5010   (gnus-group-position-point))
5011
5012 (defun gnus-group-mark-region (unmark beg end)
5013   "Mark all groups between point and mark.
5014 If UNMARK, remove the mark instead."
5015   (interactive "P\nr")
5016   (let ((num (count-lines beg end)))
5017     (save-excursion
5018       (goto-char beg)
5019       (- num (gnus-group-mark-group num unmark)))))
5020
5021 (defun gnus-group-mark-buffer (&optional unmark)
5022   "Mark all groups in the buffer.
5023 If UNMARK, remove the mark instead."
5024   (interactive "P")
5025   (gnus-group-mark-region unmark (point-min) (point-max)))
5026
5027 (defun gnus-group-mark-regexp (regexp)
5028   "Mark all groups that match some regexp."
5029   (interactive "sMark (regexp): ")
5030   (let ((alist (cdr gnus-newsrc-alist))
5031         group)
5032     (while alist
5033       (when (string-match regexp (setq group (gnus-info-group (pop alist))))
5034         (gnus-group-set-mark group))))
5035   (gnus-group-position-point))
5036
5037 (defun gnus-group-remove-mark (group)
5038   "Remove the process mark from GROUP and move point there.
5039 Return nil if the group isn't displayed."
5040   (if (gnus-group-goto-group group)
5041       (save-excursion
5042         (gnus-group-mark-group 1 'unmark t)
5043         t)
5044     (setq gnus-group-marked
5045           (delete group gnus-group-marked))
5046     nil))
5047
5048 (defun gnus-group-set-mark (group)
5049   "Set the process mark on GROUP."
5050   (if (gnus-group-goto-group group)
5051       (save-excursion
5052         (gnus-group-mark-group 1 nil t))
5053     (setq gnus-group-marked
5054           (cons group (delete group gnus-group-marked)))))
5055
5056 (defun gnus-group-universal-argument (arg &optional groups func)
5057   "Perform any command on all groups accoring to the process/prefix convention."
5058   (interactive "P")
5059   (let ((groups (or groups (gnus-group-process-prefix arg)))
5060         group func)
5061     (if (eq (setq func (or func
5062                            (key-binding
5063                             (read-key-sequence
5064                              (substitute-command-keys
5065                               "\\<gnus-group-mode-map>\\[gnus-group-universal-argument]")))))
5066             'undefined)
5067         (progn
5068           (message "Undefined key")
5069           (ding))
5070       (while groups
5071         (gnus-group-remove-mark (setq group (pop groups)))
5072         (command-execute func))))
5073   (gnus-group-position-point))
5074
5075 (defun gnus-group-process-prefix (n)
5076   "Return a list of groups to work on.
5077 Take into consideration N (the prefix) and the list of marked groups."
5078   (cond
5079    (n
5080     (setq n (prefix-numeric-value n))
5081     ;; There is a prefix, so we return a list of the N next
5082     ;; groups.
5083     (let ((way (if (< n 0) -1 1))
5084           (n (abs n))
5085           group groups)
5086       (save-excursion
5087         (while (and (> n 0)
5088                     (setq group (gnus-group-group-name)))
5089           (setq groups (cons group groups))
5090           (setq n (1- n))
5091           (gnus-group-next-group way)))
5092       (nreverse groups)))
5093    ((and (boundp 'transient-mark-mode)
5094          transient-mark-mode
5095          mark-active)
5096     ;; Work on the region between point and mark.
5097     (let ((max (max (point) (mark)))
5098           groups)
5099       (save-excursion
5100         (goto-char (min (point) (mark)))
5101         (while
5102             (and
5103              (push (gnus-group-group-name) groups)
5104              (zerop (gnus-group-next-group 1))
5105              (< (point) max)))
5106         (nreverse groups))))
5107    (gnus-group-marked
5108     ;; No prefix, but a list of marked articles.
5109     (reverse gnus-group-marked))
5110    (t
5111     ;; Neither marked articles or a prefix, so we return the
5112     ;; current group.
5113     (let ((group (gnus-group-group-name)))
5114       (and group (list group))))))
5115
5116 ;; Selecting groups.
5117
5118 (defun gnus-group-read-group (&optional all no-article group)
5119   "Read news in this newsgroup.
5120 If the prefix argument ALL is non-nil, already read articles become
5121 readable.  IF ALL is a number, fetch this number of articles.  If the
5122 optional argument NO-ARTICLE is non-nil, no article will be
5123 auto-selected upon group entry.  If GROUP is non-nil, fetch that
5124 group."
5125   (interactive "P")
5126   (let ((group (or group (gnus-group-group-name)))
5127         number active marked entry)
5128     (or group (error "No group on current line"))
5129     (setq marked (nth 3 (nth 2 (setq entry (gnus-gethash
5130                                             group gnus-newsrc-hashtb)))))
5131     ;; This group might be a dead group.  In that case we have to get
5132     ;; the number of unread articles from `gnus-active-hashtb'.
5133     (setq number
5134           (cond ((numberp all) all)
5135                 (entry (car entry))
5136                 ((setq active (gnus-active group))
5137                  (- (1+ (cdr active)) (car active)))))
5138     (gnus-summary-read-group
5139      group (or all (and (numberp number)
5140                         (zerop (+ number (length (cdr (assq 'tick marked)))
5141                                   (length (cdr (assq 'dormant marked)))))))
5142      no-article)))
5143
5144 (defun gnus-group-select-group (&optional all)
5145   "Select this newsgroup.
5146 No article is selected automatically.
5147 If ALL is non-nil, already read articles become readable.
5148 If ALL is a number, fetch this number of articles."
5149   (interactive "P")
5150   (gnus-group-read-group all t))
5151
5152 (defun gnus-group-quick-select-group (&optional all)
5153   "Select the current group \"quickly\".
5154 This means that no highlighting or scoring will be performed."
5155   (interactive "P")
5156   (let (gnus-visual
5157         gnus-score-find-score-files-function
5158         gnus-apply-kill-hook
5159         gnus-summary-expunge-below)
5160     (gnus-group-read-group all t)))
5161
5162 (defun gnus-group-visible-select-group (&optional all)
5163   "Select the current group without hiding any articles."
5164   (interactive "P")
5165   (let ((gnus-inhibit-limiting t))
5166     (gnus-group-read-group all t)))
5167
5168 ;;;###autoload
5169 (defun gnus-fetch-group (group)
5170   "Start Gnus if necessary and enter GROUP.
5171 Returns whether the fetching was successful or not."
5172   (interactive "sGroup name: ")
5173   (or (get-buffer gnus-group-buffer)
5174       (gnus))
5175   (gnus-group-read-group nil nil group))
5176
5177 ;; Enter a group that is not in the group buffer.  Non-nil is returned
5178 ;; if selection was successful.
5179 (defun gnus-group-read-ephemeral-group
5180   (group method &optional activate quit-config)
5181   (let ((group (if (gnus-group-foreign-p group) group
5182                  (gnus-group-prefixed-name group method))))
5183     (gnus-sethash
5184      group
5185      `(t nil (,group ,gnus-level-default-subscribed nil nil ,method
5186                      ((quit-config . ,(if quit-config quit-config
5187                                         (cons (current-buffer) 'summary))))))
5188      gnus-newsrc-hashtb)
5189     (set-buffer gnus-group-buffer)
5190     (or (gnus-check-server method)
5191         (error "Unable to contact server: %s" (gnus-status-message method)))
5192     (if activate (or (gnus-request-group group)
5193                      (error "Couldn't request group")))
5194     (condition-case ()
5195         (gnus-group-read-group t t group)
5196       (error nil)
5197       (quit nil))))
5198
5199 (defun gnus-group-jump-to-group (group)
5200   "Jump to newsgroup GROUP."
5201   (interactive
5202    (list (completing-read
5203           "Group: " gnus-active-hashtb nil
5204           (memq gnus-select-method gnus-have-read-active-file))))
5205
5206   (if (equal group "")
5207       (error "Empty group name"))
5208
5209   (let ((b (text-property-any
5210             (point-min) (point-max)
5211             'gnus-group (gnus-intern-safe group gnus-active-hashtb))))
5212     (unless (gnus-ephemeral-group-p group)
5213       (if b
5214           ;; Either go to the line in the group buffer...
5215           (goto-char b)
5216         ;; ... or insert the line.
5217         (or
5218          (gnus-active group)
5219          (gnus-activate-group group)
5220          (error "%s error: %s" group (gnus-status-message group)))
5221
5222         (gnus-group-update-group group)
5223         (goto-char (text-property-any
5224                     (point-min) (point-max)
5225                     'gnus-group (gnus-intern-safe group gnus-active-hashtb)))))
5226     ;; Adjust cursor point.
5227     (gnus-group-position-point)))
5228
5229 (defun gnus-group-goto-group (group)
5230   "Goto to newsgroup GROUP."
5231   (when group
5232     (let ((b (text-property-any (point-min) (point-max)
5233                                 'gnus-group (gnus-intern-safe
5234                                              group gnus-active-hashtb))))
5235       (and b (goto-char b)))))
5236
5237 (defun gnus-group-next-group (n)
5238   "Go to next N'th newsgroup.
5239 If N is negative, search backward instead.
5240 Returns the difference between N and the number of skips actually
5241 done."
5242   (interactive "p")
5243   (gnus-group-next-unread-group n t))
5244
5245 (defun gnus-group-next-unread-group (n &optional all level)
5246   "Go to next N'th unread newsgroup.
5247 If N is negative, search backward instead.
5248 If ALL is non-nil, choose any newsgroup, unread or not.
5249 If LEVEL is non-nil, choose the next group with level LEVEL, or, if no
5250 such group can be found, the next group with a level higher than
5251 LEVEL.
5252 Returns the difference between N and the number of skips actually
5253 made."
5254   (interactive "p")
5255   (let ((backward (< n 0))
5256         (n (abs n)))
5257     (while (and (> n 0)
5258                 (gnus-group-search-forward
5259                  backward (or (not gnus-group-goto-unread) all) level))
5260       (setq n (1- n)))
5261     (if (/= 0 n) (gnus-message 7 "No more%s newsgroups%s" (if all "" " unread")
5262                                (if level " on this level or higher" "")))
5263     n))
5264
5265 (defun gnus-group-prev-group (n)
5266   "Go to previous N'th newsgroup.
5267 Returns the difference between N and the number of skips actually
5268 done."
5269   (interactive "p")
5270   (gnus-group-next-unread-group (- n) t))
5271
5272 (defun gnus-group-prev-unread-group (n)
5273   "Go to previous N'th unread newsgroup.
5274 Returns the difference between N and the number of skips actually
5275 done."
5276   (interactive "p")
5277   (gnus-group-next-unread-group (- n)))
5278
5279 (defun gnus-group-next-unread-group-same-level (n)
5280   "Go to next N'th unread newsgroup on the same level.
5281 If N is negative, search backward instead.
5282 Returns the difference between N and the number of skips actually
5283 done."
5284   (interactive "p")
5285   (gnus-group-next-unread-group n t (gnus-group-group-level))
5286   (gnus-group-position-point))
5287
5288 (defun gnus-group-prev-unread-group-same-level (n)
5289   "Go to next N'th unread newsgroup on the same level.
5290 Returns the difference between N and the number of skips actually
5291 done."
5292   (interactive "p")
5293   (gnus-group-next-unread-group (- n) t (gnus-group-group-level))
5294   (gnus-group-position-point))
5295
5296 (defun gnus-group-best-unread-group (&optional exclude-group)
5297   "Go to the group with the highest level.
5298 If EXCLUDE-GROUP, do not go to that group."
5299   (interactive)
5300   (goto-char (point-min))
5301   (let ((best 100000)
5302         unread best-point)
5303     (while (setq unread (get-text-property (point) 'gnus-unread))
5304       (if (and (numberp unread) (> unread 0))
5305           (progn
5306             (if (and (< (get-text-property (point) 'gnus-level) best)
5307                      (or (not exclude-group)
5308                          (not (equal exclude-group (gnus-group-group-name)))))
5309                 (progn
5310                   (setq best (get-text-property (point) 'gnus-level))
5311                   (setq best-point (point))))))
5312       (forward-line 1))
5313     (if best-point (goto-char best-point))
5314     (gnus-summary-position-point)
5315     (and best-point (gnus-group-group-name))))
5316
5317 (defun gnus-group-first-unread-group ()
5318   "Go to the first group with unread articles."
5319   (interactive)
5320   (prog1
5321       (let ((opoint (point))
5322             unread)
5323         (goto-char (point-min))
5324         (if (or (eq (setq unread (gnus-group-group-unread)) t) ; Not active.
5325                 (and (numberp unread)   ; Not a topic.
5326                      (not (zerop unread))) ; Has unread articles.
5327                 (zerop (gnus-group-next-unread-group 1))) ; Next unread group.
5328             (point)                     ; Success.
5329           (goto-char opoint)
5330           nil))                         ; Not success.
5331     (gnus-group-position-point)))
5332
5333 (defun gnus-group-enter-server-mode ()
5334   "Jump to the server buffer."
5335   (interactive)
5336   (gnus-enter-server-buffer))
5337
5338 (defun gnus-group-make-group (name &optional method address)
5339   "Add a new newsgroup.
5340 The user will be prompted for a NAME, for a select METHOD, and an
5341 ADDRESS."
5342   (interactive
5343    (cons
5344     (read-string "Group name: ")
5345     (let ((method
5346            (completing-read
5347             "Method: " (append gnus-valid-select-methods gnus-server-alist)
5348             nil t)))
5349       (if (assoc method gnus-valid-select-methods)
5350           (list method
5351                 (if (memq 'prompt-address
5352                           (assoc method gnus-valid-select-methods))
5353                     (read-string "Address: ")
5354                   ""))
5355         (list method "")))))
5356
5357   (save-excursion
5358     (set-buffer gnus-group-buffer)
5359     (let* ((meth (and method (if address (list (intern method) address)
5360                                method)))
5361            (nname (if method (gnus-group-prefixed-name name meth) name))
5362            info)
5363       (and (gnus-gethash nname gnus-newsrc-hashtb)
5364            (error "Group %s already exists" nname))
5365       (gnus-group-change-level
5366        (setq info (list t nname gnus-level-default-subscribed nil nil meth))
5367        gnus-level-default-subscribed gnus-level-killed
5368        (and (gnus-group-group-name)
5369             (gnus-gethash (gnus-group-group-name)
5370                           gnus-newsrc-hashtb))
5371        t)
5372       (gnus-set-active nname (cons 1 0))
5373       (or (gnus-ephemeral-group-p name)
5374           (gnus-dribble-enter
5375            (concat "(gnus-group-set-info '" (prin1-to-string (cdr info)) ")")))
5376       (gnus-group-insert-group-line-info nname)
5377
5378       (when (assoc (symbol-name (car meth)) gnus-valid-select-methods)
5379         (require (car meth)))
5380       (gnus-check-server meth)
5381       (and (gnus-check-backend-function 'request-create-group nname)
5382            (gnus-request-create-group nname))
5383       t)))
5384
5385 (defun gnus-group-delete-group (group &optional force)
5386   "Delete the current group.
5387 If FORCE (the prefix) is non-nil, all the articles in the group will
5388 be deleted.  This is \"deleted\" as in \"removed forever from the face
5389 of the Earth\".  There is no undo."
5390   (interactive
5391    (list (gnus-group-group-name)
5392          current-prefix-arg))
5393   (or group (error "No group to rename"))
5394   (or (gnus-check-backend-function 'request-delete-group group)
5395       (error "This backend does not support group deletion"))
5396   (prog1
5397       (if (not (gnus-yes-or-no-p
5398                 (format
5399                  "Do you really want to delete %s%s? "
5400                  group (if force " and all its contents" ""))))
5401           () ; Whew!
5402         (gnus-message 6 "Deleting group %s..." group)
5403         (if (not (gnus-request-delete-group group force))
5404             (progn
5405               (gnus-message 3 "Couldn't delete group %s" group)
5406               (ding))
5407           (gnus-message 6 "Deleting group %s...done" group)
5408           (gnus-group-goto-group group)
5409           (gnus-group-kill-group 1 t)
5410           t))
5411     (gnus-group-position-point)))
5412
5413 (defun gnus-group-rename-group (group new-name)
5414   (interactive
5415    (list
5416     (gnus-group-group-name)
5417     (progn
5418       (or (gnus-check-backend-function
5419            'request-rename-group (gnus-group-group-name))
5420           (error "This backend does not support renaming groups"))
5421       (read-string "New group name: "))))
5422
5423   (or (gnus-check-backend-function 'request-rename-group group)
5424       (error "This backend does not support renaming groups"))
5425
5426   (or group (error "No group to rename"))
5427   (and (string-match "^[ \t]*$" new-name)
5428        (error "Not a valid group name"))
5429
5430   ;; We find the proper prefixed name.
5431   (setq new-name
5432         (gnus-group-prefixed-name
5433          (gnus-group-real-name new-name)
5434          (gnus-info-method (gnus-get-info group))))
5435
5436   (gnus-message 6 "Renaming group %s to %s..." group new-name)
5437   (prog1
5438       (if (not (gnus-request-rename-group group new-name))
5439           (progn
5440             (gnus-message 3 "Couldn't rename group %s to %s" group new-name)
5441             (ding))
5442         ;; We rename the group internally by killing it...
5443         (gnus-group-goto-group group)
5444         (gnus-group-kill-group)
5445         ;; ... changing its name ...
5446         (setcar (cdar gnus-list-of-killed-groups) new-name)
5447         ;; ... and then yanking it.  Magic!
5448         (gnus-group-yank-group)
5449         (gnus-set-active new-name (gnus-active group))
5450         (gnus-message 6 "Renaming group %s to %s...done" group new-name)
5451         new-name)
5452     (gnus-group-position-point)))
5453
5454 (defun gnus-group-edit-group (group &optional part)
5455   "Edit the group on the current line."
5456   (interactive (list (gnus-group-group-name)))
5457   (let* ((part (or part 'info))
5458          (done-func `(lambda ()
5459                        "Exit editing mode and update the information."
5460                        (interactive)
5461                        (gnus-group-edit-group-done ',part ,group)))
5462          (winconf (current-window-configuration))
5463          info)
5464     (or group (error "No group on current line"))
5465     (or (setq info (gnus-get-info group))
5466         (error "Killed group; can't be edited"))
5467     (set-buffer (get-buffer-create gnus-group-edit-buffer))
5468     (gnus-configure-windows 'edit-group)
5469     (gnus-add-current-to-buffer-list)
5470     (emacs-lisp-mode)
5471     ;; Suggested by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
5472     (use-local-map (copy-keymap emacs-lisp-mode-map))
5473     (local-set-key "\C-c\C-c" done-func)
5474     (make-local-variable 'gnus-prev-winconf)
5475     (setq gnus-prev-winconf winconf)
5476     (erase-buffer)
5477     (insert
5478      (cond
5479       ((eq part 'method)
5480        ";; Type `C-c C-c' after editing the select method.\n\n")
5481       ((eq part 'params)
5482        ";; Type `C-c C-c' after editing the group parameters.\n\n")
5483       ((eq part 'info)
5484        ";; Type `C-c C-c' after editing the group info.\n\n")))
5485     (insert
5486      (pp-to-string
5487       (cond ((eq part 'method)
5488              (or (gnus-info-method info) "native"))
5489             ((eq part 'params)
5490              (gnus-info-params info))
5491             (t info)))
5492      "\n")))
5493
5494 (defun gnus-group-edit-group-method (group)
5495   "Edit the select method of GROUP."
5496   (interactive (list (gnus-group-group-name)))
5497   (gnus-group-edit-group group 'method))
5498
5499 (defun gnus-group-edit-group-parameters (group)
5500   "Edit the group parameters of GROUP."
5501   (interactive (list (gnus-group-group-name)))
5502   (gnus-group-edit-group group 'params))
5503
5504 (defun gnus-group-edit-group-done (part group)
5505   "Get info from buffer, update variables and jump to the group buffer."
5506   (set-buffer (get-buffer-create gnus-group-edit-buffer))
5507   (goto-char (point-min))
5508   (let* ((form (read (current-buffer)))
5509          (winconf gnus-prev-winconf)
5510          (method (cond ((eq part 'info) (nth 4 form))
5511                        ((eq part 'method) form)
5512                        (t nil)))
5513          (info (cond ((eq part 'info) form)
5514                      ((eq part 'method) (gnus-get-info group))
5515                      (t nil)))
5516          (new-group (if info
5517                       (if (or (not method)
5518                               (gnus-server-equal
5519                                gnus-select-method method))
5520                           (gnus-group-real-name (car info))
5521                         (gnus-group-prefixed-name
5522                          (gnus-group-real-name (car info)) method))
5523                       nil)))
5524     (when (and new-group
5525                (not (equal new-group group)))
5526       (when (gnus-group-goto-group group)
5527         (gnus-group-kill-group 1))
5528       (gnus-activate-group new-group))
5529     ;; Set the info.
5530     (if (and info new-group)
5531         (progn
5532           (setq info (gnus-copy-sequence info))
5533           (setcar info new-group)
5534           (unless (gnus-server-equal method "native")
5535             (unless (nthcdr 3 info)
5536               (nconc info (list nil nil)))
5537             (unless (nthcdr 4 info)
5538               (nconc info (list nil)))
5539             (gnus-info-set-method info method))
5540           (gnus-group-set-info info))
5541       (gnus-group-set-info form (or new-group group) part))
5542     (kill-buffer (current-buffer))
5543     (and winconf (set-window-configuration winconf))
5544     (set-buffer gnus-group-buffer)
5545     (gnus-group-update-group (or new-group group))
5546     (gnus-group-position-point)))
5547
5548 (defun gnus-group-make-help-group ()
5549   "Create the Gnus documentation group."
5550   (interactive)
5551   (let ((path load-path)
5552         (name (gnus-group-prefixed-name "gnus-help" '(nndoc "gnus-help")))
5553         file dir)
5554     (and (gnus-gethash name gnus-newsrc-hashtb)
5555          (error "Documentation group already exists"))
5556     (while path
5557       (setq dir (file-name-as-directory (expand-file-name (pop path)))
5558             file nil)
5559       (when (or (file-exists-p (setq file (concat dir "gnus-tut.txt")))
5560                 (file-exists-p
5561                  (setq file (concat (file-name-directory
5562                                      (directory-file-name dir))
5563                                     "etc/gnus-tut.txt"))))
5564         (setq path nil)))
5565     (if (not file)
5566         (message "Couldn't find doc group")
5567       (gnus-group-make-group
5568        (gnus-group-real-name name)
5569        (list 'nndoc name
5570              (list 'nndoc-address file)
5571              (list 'nndoc-article-type 'mbox)))))
5572   (gnus-group-position-point))
5573
5574 (defun gnus-group-make-doc-group (file type)
5575   "Create a group that uses a single file as the source."
5576   (interactive
5577    (list (read-file-name "File name: ")
5578          (and current-prefix-arg 'ask)))
5579   (when (eq type 'ask)
5580     (let ((err "")
5581           char found)
5582       (while (not found)
5583         (message
5584          "%sFile type (mbox, babyl, digest, forward, mmfd, guess) [mbdfag]: "
5585          err)
5586         (setq found (cond ((= (setq char (read-char)) ?m) 'mbox)
5587                           ((= char ?b) 'babyl)
5588                           ((= char ?d) 'digest)
5589                           ((= char ?f) 'forward)
5590                           ((= char ?a) 'mmfd)
5591                           (t (setq err (format "%c unknown. " char))
5592                              nil))))
5593       (setq type found)))
5594   (let* ((file (expand-file-name file))
5595          (name (gnus-generate-new-group-name
5596                 (gnus-group-prefixed-name
5597                  (file-name-nondirectory file) '(nndoc "")))))
5598     (gnus-group-make-group
5599      (gnus-group-real-name name)
5600      (list 'nndoc name
5601            (list 'nndoc-address file)
5602            (list 'nndoc-article-type (or type 'guess))))
5603     (forward-line -1)
5604     (gnus-group-position-point)))
5605
5606 (defun gnus-group-make-archive-group (&optional all)
5607   "Create the (ding) Gnus archive group of the most recent articles.
5608 Given a prefix, create a full group."
5609   (interactive "P")
5610   (let ((group (gnus-group-prefixed-name
5611                 (if all "ding.archives" "ding.recent") '(nndir ""))))
5612     (and (gnus-gethash group gnus-newsrc-hashtb)
5613          (error "Archive group already exists"))
5614     (gnus-group-make-group
5615      (gnus-group-real-name group)
5616      (list 'nndir (if all "hpc" "edu")
5617            (list 'nndir-directory
5618                  (if all gnus-group-archive-directory
5619                    gnus-group-recent-archive-directory)))))
5620   (forward-line -1)
5621   (gnus-group-position-point))
5622
5623 (defun gnus-group-make-directory-group (dir)
5624   "Create an nndir group.
5625 The user will be prompted for a directory.  The contents of this
5626 directory will be used as a newsgroup.  The directory should contain
5627 mail messages or news articles in files that have numeric names."
5628   (interactive
5629    (list (read-file-name "Create group from directory: ")))
5630   (or (file-exists-p dir) (error "No such directory"))
5631   (or (file-directory-p dir) (error "Not a directory"))
5632   (let ((ext "")
5633         (i 0)
5634         group)
5635     (while (or (not group) (gnus-gethash group gnus-newsrc-hashtb))
5636       (setq group
5637             (gnus-group-prefixed-name
5638              (concat (file-name-as-directory (directory-file-name dir))
5639                      ext)
5640              '(nndir "")))
5641       (setq ext (format "<%d>" (setq i (1+ i)))))
5642     (gnus-group-make-group
5643      (gnus-group-real-name group)
5644      (list 'nndir group (list 'nndir-directory dir))))
5645   (forward-line -1)
5646   (gnus-group-position-point))
5647
5648 (defun gnus-group-make-kiboze-group (group address scores)
5649   "Create an nnkiboze group.
5650 The user will be prompted for a name, a regexp to match groups, and
5651 score file entries for articles to include in the group."
5652   (interactive
5653    (list
5654     (read-string "nnkiboze group name: ")
5655     (read-string "Source groups (regexp): ")
5656     (let ((headers (mapcar (lambda (group) (list group))
5657                            '("subject" "from" "number" "date" "message-id"
5658                              "references" "chars" "lines" "xref"
5659                              "followup" "all" "body" "head")))
5660           scores header regexp regexps)
5661       (while (not (equal "" (setq header (completing-read
5662                                           "Match on header: " headers nil t))))
5663         (setq regexps nil)
5664         (while (not (equal "" (setq regexp (read-string
5665                                             (format "Match on %s (string): "
5666                                                     header)))))
5667           (setq regexps (cons (list regexp nil nil 'r) regexps)))
5668         (setq scores (cons (cons header regexps) scores)))
5669       scores)))
5670   (gnus-group-make-group group "nnkiboze" address)
5671   (save-excursion
5672     (gnus-set-work-buffer)
5673     (let (emacs-lisp-mode-hook)
5674       (pp scores (current-buffer)))
5675     (write-region (point-min) (point-max)
5676                   (gnus-score-file-name (concat "nnkiboze:" group))))
5677   (forward-line -1)
5678   (gnus-group-position-point))
5679
5680 (defun gnus-group-add-to-virtual (n vgroup)
5681   "Add the current group to a virtual group."
5682   (interactive
5683    (list current-prefix-arg
5684          (completing-read "Add to virtual group: " gnus-newsrc-hashtb nil t
5685                           "nnvirtual:")))
5686   (or (eq (car (gnus-find-method-for-group vgroup)) 'nnvirtual)
5687       (error "%s is not an nnvirtual group" vgroup))
5688   (let* ((groups (gnus-group-process-prefix n))
5689          (method (gnus-info-method (gnus-get-info vgroup))))
5690     (setcar (cdr method)
5691             (concat
5692              (nth 1 method) "\\|"
5693              (mapconcat
5694               (lambda (s)
5695                 (gnus-group-remove-mark s)
5696                 (concat "\\(^" (regexp-quote s) "$\\)"))
5697               groups "\\|"))))
5698   (gnus-group-position-point))
5699
5700 (defun gnus-group-make-empty-virtual (group)
5701   "Create a new, fresh, empty virtual group."
5702   (interactive "sCreate new, empty virtual group: ")
5703   (let* ((method (list 'nnvirtual "^$"))
5704          (pgroup (gnus-group-prefixed-name group method)))
5705     ;; Check whether it exists already.
5706     (and (gnus-gethash pgroup gnus-newsrc-hashtb)
5707          (error "Group %s already exists." pgroup))
5708     ;; Subscribe the new group after the group on the current line.
5709     (gnus-subscribe-group pgroup (gnus-group-group-name) method)
5710     (gnus-group-update-group pgroup)
5711     (forward-line -1)
5712     (gnus-group-position-point)))
5713
5714 (defun gnus-group-enter-directory (dir)
5715   "Enter an ephemeral nneething group."
5716   (interactive "DDirectory to read: ")
5717   (let* ((method (list 'nneething dir))
5718          (leaf (gnus-group-prefixed-name
5719                 (file-name-nondirectory (directory-file-name dir))
5720                 method))
5721          (name (gnus-generate-new-group-name leaf)))
5722     (let ((nneething-read-only t))
5723       (or (gnus-group-read-ephemeral-group
5724            name method t
5725            (cons (current-buffer) (if (eq major-mode 'gnus-summary-mode)
5726                                       'summary 'group)))
5727           (error "Couldn't enter %s" dir)))))
5728
5729 ;; Group sorting commands
5730 ;; Suggested by Joe Hildebrand <hildjj@idaho.fuentez.com>.
5731
5732 (defun gnus-group-sort-groups (func &optional reverse)
5733   "Sort the group buffer according to FUNC.
5734 If REVERSE, reverse the sorting order."
5735   (interactive (list gnus-group-sort-function
5736                      current-prefix-arg))
5737   (let ((func (cond 
5738                ((not (listp func)) func)
5739                ((null func) func)
5740                ((= 1 (length func)) (car func))
5741                (t `(lambda (t1 t2)
5742                      ,(gnus-make-sort-function 
5743                        (reverse func)))))))
5744     ;; We peel off the dummy group from the alist.
5745     (when func
5746       (when (equal (car (gnus-info-group gnus-newsrc-alist)) "dummy.group")
5747         (pop gnus-newsrc-alist))
5748       ;; Do the sorting.
5749       (setq gnus-newsrc-alist
5750             (sort gnus-newsrc-alist func))
5751       (when reverse
5752         (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist)))
5753       ;; Regenerate the hash table.
5754       (gnus-make-hashtable-from-newsrc-alist)
5755       (gnus-group-list-groups))))
5756
5757 (defun gnus-group-sort-groups-by-alphabet (&optional reverse)
5758   "Sort the group buffer alphabetically by group name.
5759 If REVERSE, sort in reverse order."
5760   (interactive "P")
5761   (gnus-group-sort-groups 'gnus-group-sort-by-alphabet reverse))
5762
5763 (defun gnus-group-sort-groups-by-unread (&optional reverse)
5764   "Sort the group buffer by number of unread articles.
5765 If REVERSE, sort in reverse order."
5766   (interactive "P")
5767   (gnus-group-sort-groups 'gnus-group-sort-by-unread reverse))
5768
5769 (defun gnus-group-sort-groups-by-level (&optional reverse)
5770   "Sort the group buffer by group level.
5771 If REVERSE, sort in reverse order."
5772   (interactive "P")
5773   (gnus-group-sort-groups 'gnus-group-sort-by-level reverse))
5774
5775 (defun gnus-group-sort-groups-by-score (&optional reverse)
5776   "Sort the group buffer by group score.
5777 If REVERSE, sort in reverse order."
5778   (interactive "P")
5779   (gnus-group-sort-groups 'gnus-group-sort-by-score reverse))
5780
5781 (defun gnus-group-sort-groups-by-rank (&optional reverse)
5782   "Sort the group buffer by group rank.
5783 If REVERSE, sort in reverse order."
5784   (interactive "P")
5785   (gnus-group-sort-groups 'gnus-group-sort-by-rank reverse))
5786
5787 (defun gnus-group-sort-groups-by-method (&optional reverse)
5788   "Sort the group buffer alphabetically by backend name.
5789 If REVERSE, sort in reverse order."
5790   (interactive "P")
5791   (gnus-group-sort-groups 'gnus-group-sort-by-method reverse))
5792
5793 (defun gnus-group-sort-by-alphabet (info1 info2)
5794   "Sort alphabetically."
5795   (string< (gnus-info-group info1) (gnus-info-group info2)))
5796
5797 (defun gnus-group-sort-by-unread (info1 info2)
5798   "Sort by number of unread articles."
5799   (let ((n1 (car (gnus-gethash (gnus-info-group info1) gnus-newsrc-hashtb)))
5800         (n2 (car (gnus-gethash (gnus-info-group info2) gnus-newsrc-hashtb))))
5801     (< (or (and (numberp n1) n1) 0)
5802        (or (and (numberp n2) n2) 0))))
5803
5804 (defun gnus-group-sort-by-level (info1 info2)
5805   "Sort by level."
5806   (< (gnus-info-level info1) (gnus-info-level info2)))
5807
5808 (defun gnus-group-sort-by-method (info1 info2)
5809   "Sort alphabetically by backend name."
5810   (string< (symbol-name (car (gnus-find-method-for-group
5811                               (gnus-info-group info1) info1)))
5812            (symbol-name (car (gnus-find-method-for-group
5813                               (gnus-info-group info2) info2)))))
5814
5815 (defun gnus-group-sort-by-score (info1 info2)
5816   "Sort by group score."
5817   (< (gnus-info-score info1) (gnus-info-score info2)))
5818
5819 (defun gnus-group-sort-by-rank (info1 info2)
5820   "Sort by level and score."
5821   (let ((level1 (gnus-info-level info1))
5822         (level2 (gnus-info-level info2)))
5823     (or (< level1 level2)
5824         (and (= level1 level2)
5825              (< (gnus-info-score info1) (gnus-info-score info2))))))
5826
5827 ;; Group catching up.
5828
5829 (defun gnus-group-catchup-current (&optional n all)
5830   "Mark all articles not marked as unread in current newsgroup as read.
5831 If prefix argument N is numeric, the ARG next newsgroups will be
5832 caught up.  If ALL is non-nil, marked articles will also be marked as
5833 read.  Cross references (Xref: header) of articles are ignored.
5834 The difference between N and actual number of newsgroups that were
5835 caught up is returned."
5836   (interactive "P")
5837   (if (not (or (not gnus-interactive-catchup) ;Without confirmation?
5838                gnus-expert-user
5839                (gnus-y-or-n-p
5840                 (if all
5841                     "Do you really want to mark all articles as read? "
5842                   "Mark all unread articles as read? "))))
5843       n
5844     (let ((groups (gnus-group-process-prefix n))
5845           (ret 0))
5846       (while groups
5847         ;; Virtual groups have to be given special treatment.
5848         (let ((method (gnus-find-method-for-group (car groups))))
5849           (if (eq 'nnvirtual (car method))
5850               (nnvirtual-catchup-group
5851                (gnus-group-real-name (car groups)) (nth 1 method) all)))
5852         (gnus-group-remove-mark (car groups))
5853         (if (prog1
5854                 (gnus-group-goto-group (car groups))
5855               (gnus-group-catchup (car groups) all))
5856             (gnus-group-update-group-line)
5857           (setq ret (1+ ret)))
5858         (setq groups (cdr groups)))
5859       (gnus-group-next-unread-group 1)
5860       ret)))
5861
5862 (defun gnus-group-catchup-current-all (&optional n)
5863   "Mark all articles in current newsgroup as read.
5864 Cross references (Xref: header) of articles are ignored."
5865   (interactive "P")
5866   (gnus-group-catchup-current n 'all))
5867
5868 (defun gnus-group-catchup (group &optional all)
5869   "Mark all articles in GROUP as read.
5870 If ALL is non-nil, all articles are marked as read.
5871 The return value is the number of articles that were marked as read,
5872 or nil if no action could be taken."
5873   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
5874          (num (car entry)))
5875     ;; Do the updating only if the newsgroup isn't killed.
5876     (if (not (numberp (car entry)))
5877         (gnus-message 1 "Can't catch up; non-active group")
5878       ;; Do auto-expirable marks if that's required.
5879       (when (gnus-group-auto-expirable-p group)
5880         (gnus-add-marked-articles
5881          group 'expire (gnus-list-of-unread-articles group))
5882         (when all
5883           (let ((marks (nth 3 (nth 2 entry))))
5884             (gnus-add-marked-articles
5885              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks))))
5886             (gnus-add-marked-articles
5887              group 'expire (gnus-uncompress-range (cdr (assq 'tick marks)))))))
5888       (when entry
5889         (gnus-update-read-articles group nil)
5890         ;; Also nix out the lists of marks and dormants.
5891         (when all
5892           (gnus-add-marked-articles group 'tick nil nil 'force)
5893           (gnus-add-marked-articles group 'dormant nil nil 'force))
5894         (run-hooks 'gnus-group-catchup-group-hook)
5895         num))))
5896
5897 (defun gnus-group-expire-articles (&optional n)
5898   "Expire all expirable articles in the current newsgroup."
5899   (interactive "P")
5900   (let ((groups (gnus-group-process-prefix n))
5901         group)
5902     (unless groups
5903       (error "No groups to expire"))
5904     (while (setq group (pop groups))
5905       (gnus-group-remove-mark group)
5906       (when (gnus-check-backend-function 'request-expire-articles group)
5907         (gnus-message 6 "Expiring articles in %s..." group)
5908         (let* ((info (gnus-get-info group))
5909                (expirable (if (gnus-group-total-expirable-p group)
5910                               (cons nil (gnus-list-of-read-articles group))
5911                             (assq 'expire (gnus-info-marks info))))
5912                (expiry-wait (gnus-group-get-parameter group 'expiry-wait)))
5913           (when expirable
5914             (setcdr
5915              expirable
5916              (gnus-compress-sequence
5917               (if expiry-wait
5918                   ;; We set the expiry variables to the groupp
5919                   ;; parameter. 
5920                   (let ((nnmail-expiry-wait-function nil)
5921                         (nnmail-expiry-wait expiry-wait))
5922                     (gnus-request-expire-articles
5923                      (gnus-uncompress-sequence (cdr expirable)) group))
5924                 ;; Just expire using the normal expiry values.
5925                 (gnus-request-expire-articles
5926                  (gnus-uncompress-sequence (cdr expirable)) group)))))
5927           (gnus-message 6 "Expiring articles in %s...done" group)))
5928       (gnus-group-position-point))))
5929
5930 (defun gnus-group-expire-all-groups ()
5931   "Expire all expirable articles in all newsgroups."
5932   (interactive)
5933   (save-excursion
5934     (gnus-message 5 "Expiring...")
5935     (let ((gnus-group-marked (mapcar (lambda (info) (gnus-info-group info))
5936                                      (cdr gnus-newsrc-alist))))
5937       (gnus-group-expire-articles nil)))
5938   (gnus-group-position-point)
5939   (gnus-message 5 "Expiring...done"))
5940
5941 (defun gnus-group-set-current-level (n level)
5942   "Set the level of the next N groups to LEVEL."
5943   (interactive
5944    (list
5945     current-prefix-arg
5946     (if (not (gnus-group-group-name))
5947         (error "No group on the current line")
5948       (string-to-int
5949        (let ((s (read-string
5950                  (format "Level (default %s): " (gnus-group-group-level)))))
5951          (if (string-match "^\\s-*$" s)
5952              (int-to-string (gnus-group-group-level))
5953            s))))))
5954   (or (and (>= level 1) (<= level gnus-level-killed))
5955       (error "Illegal level: %d" level))
5956   (let ((groups (gnus-group-process-prefix n))
5957         group)
5958     (while (setq group (pop groups))
5959       (gnus-group-remove-mark group)
5960       (gnus-message 6 "Changed level of %s from %d to %d"
5961                     group (or (gnus-group-group-level) gnus-level-killed)
5962                     level)
5963       (gnus-group-change-level
5964        group level (or (gnus-group-group-level) gnus-level-killed))
5965       (gnus-group-update-group-line)))
5966   (gnus-group-position-point))
5967
5968 (defun gnus-group-unsubscribe-current-group (&optional n)
5969   "Toggle subscription of the current group.
5970 If given numerical prefix, toggle the N next groups."
5971   (interactive "P")
5972   (let ((groups (gnus-group-process-prefix n))
5973         group)
5974     (while groups
5975       (setq group (car groups)
5976             groups (cdr groups))
5977       (gnus-group-remove-mark group)
5978       (gnus-group-unsubscribe-group
5979        group (if (<= (gnus-group-group-level) gnus-level-subscribed)
5980                  gnus-level-default-unsubscribed
5981                gnus-level-default-subscribed) t)
5982       (gnus-group-update-group-line))
5983     (gnus-group-next-group 1)))
5984
5985 (defun gnus-group-unsubscribe-group (group &optional level silent)
5986   "Toggle subscription to GROUP.
5987 Killed newsgroups are subscribed.  If SILENT, don't try to update the
5988 group line."
5989   (interactive
5990    (list (completing-read
5991           "Group: " gnus-active-hashtb nil
5992           (memq gnus-select-method gnus-have-read-active-file))))
5993   (let ((newsrc (gnus-gethash group gnus-newsrc-hashtb)))
5994     (cond
5995      ((string-match "^[ \t]$" group)
5996       (error "Empty group name"))
5997      (newsrc
5998       ;; Toggle subscription flag.
5999       (gnus-group-change-level
6000        newsrc (if level level (if (<= (nth 1 (nth 2 newsrc))
6001                                       gnus-level-subscribed)
6002                                   (1+ gnus-level-subscribed)
6003                                 gnus-level-default-subscribed)))
6004       (unless silent
6005         (gnus-group-update-group group)))
6006      ((and (stringp group)
6007            (or (not (memq gnus-select-method gnus-have-read-active-file))
6008                (gnus-active group)))
6009       ;; Add new newsgroup.
6010       (gnus-group-change-level
6011        group
6012        (if level level gnus-level-default-subscribed)
6013        (or (and (member group gnus-zombie-list)
6014                 gnus-level-zombie)
6015            gnus-level-killed)
6016        (and (gnus-group-group-name)
6017             (gnus-gethash (gnus-group-group-name) gnus-newsrc-hashtb)))
6018       (unless silent
6019         (gnus-group-update-group group)))
6020      (t (error "No such newsgroup: %s" group)))
6021     (gnus-group-position-point)))
6022
6023 (defun gnus-group-transpose-groups (n)
6024   "Move the current newsgroup up N places.
6025 If given a negative prefix, move down instead.  The difference between
6026 N and the number of steps taken is returned."
6027   (interactive "p")
6028   (or (gnus-group-group-name)
6029       (error "No group on current line"))
6030   (gnus-group-kill-group 1)
6031   (prog1
6032       (forward-line (- n))
6033     (gnus-group-yank-group)
6034     (gnus-group-position-point)))
6035
6036 (defun gnus-group-kill-all-zombies ()
6037   "Kill all zombie newsgroups."
6038   (interactive)
6039   (setq gnus-killed-list (nconc gnus-zombie-list gnus-killed-list))
6040   (setq gnus-zombie-list nil)
6041   (gnus-group-list-groups))
6042
6043 (defun gnus-group-kill-region (begin end)
6044   "Kill newsgroups in current region (excluding current point).
6045 The killed newsgroups can be yanked by using \\[gnus-group-yank-group]."
6046   (interactive "r")
6047   (let ((lines
6048          ;; Count lines.
6049          (save-excursion
6050            (count-lines
6051             (progn
6052               (goto-char begin)
6053               (beginning-of-line)
6054               (point))
6055             (progn
6056               (goto-char end)
6057               (beginning-of-line)
6058               (point))))))
6059     (goto-char begin)
6060     (beginning-of-line)                 ;Important when LINES < 1
6061     (gnus-group-kill-group lines)))
6062
6063 (defun gnus-group-kill-group (&optional n discard)
6064   "Kill the next N groups.
6065 The killed newsgroups can be yanked by using \\[gnus-group-yank-group].
6066 However, only groups that were alive can be yanked; already killed
6067 groups or zombie groups can't be yanked.
6068 The return value is the name of the group that was killed, or a list
6069 of groups killed."
6070   (interactive "P")
6071   (let ((buffer-read-only nil)
6072         (groups (gnus-group-process-prefix n))
6073         group entry level out)
6074     (if (< (length groups) 10)
6075         ;; This is faster when there are few groups.
6076         (while groups
6077           (push (setq group (pop groups)) out)
6078           (gnus-group-remove-mark group)
6079           (setq level (gnus-group-group-level))
6080           (gnus-delete-line)
6081           (when (and (not discard)
6082                      (setq entry (gnus-gethash group gnus-newsrc-hashtb)))
6083             (push (cons (car entry) (nth 2 entry))
6084                   gnus-list-of-killed-groups))
6085           (gnus-group-change-level
6086            (if entry entry group) gnus-level-killed (if entry nil level)))
6087       ;; If there are lots and lots of groups to be killed, we use
6088       ;; this thing instead.
6089       (let (entry)
6090         (setq groups (nreverse groups))
6091         (while groups
6092           (gnus-group-remove-mark (setq group (pop groups)))
6093           (gnus-delete-line)
6094           (cond
6095            ((setq entry (gnus-gethash group gnus-newsrc-hashtb))
6096             (push (cons (car entry) (nth 2 entry))
6097                   gnus-list-of-killed-groups)
6098             (setcdr (cdr entry) (cdddr entry)))
6099            ((member group gnus-zombie-list)
6100             (setq gnus-zombie-list (delete group gnus-zombie-list)))))
6101         (gnus-make-hashtable-from-newsrc-alist)))
6102
6103     (gnus-group-position-point)
6104     (if (< (length out) 2) (car out) (nreverse out))))
6105
6106 (defun gnus-group-yank-group (&optional arg)
6107   "Yank the last newsgroups killed with \\[gnus-group-kill-group],
6108 inserting it before the current newsgroup.  The numeric ARG specifies
6109 how many newsgroups are to be yanked.  The name of the newsgroup yanked
6110 is returned, or (if several groups are yanked) a list of yanked groups
6111 is returned."
6112   (interactive "p")
6113   (setq arg (or arg 1))
6114   (let (info group prev out)
6115     (while (>= (decf arg) 0)
6116       (if (not (setq info (pop gnus-list-of-killed-groups)))
6117           (error "No more newsgroups to yank"))
6118       (push (setq group (nth 1 info)) out)
6119       ;; Find which newsgroup to insert this one before - search
6120       ;; backward until something suitable is found.  If there are no
6121       ;; other newsgroups in this buffer, just make this newsgroup the
6122       ;; first newsgroup.
6123       (setq prev (gnus-group-group-name))
6124       (gnus-group-change-level
6125        info (nth 2 info) gnus-level-killed
6126        (and prev (gnus-gethash prev gnus-newsrc-hashtb))
6127        t)
6128       (gnus-group-insert-group-line-info group))
6129     (forward-line -1)
6130     (gnus-group-position-point)
6131     (if (< (length out) 2) (car out) (nreverse out))))
6132
6133 (defun gnus-group-kill-level (level)
6134   "Kill all groups that is on a certain LEVEL."
6135   (interactive "nKill all groups on level: ")
6136   (cond
6137    ((= level gnus-level-zombie)
6138     (setq gnus-killed-list
6139           (nconc gnus-zombie-list gnus-killed-list))
6140     (setq gnus-zombie-list nil))
6141    ((and (< level gnus-level-zombie)
6142          (> level 0)
6143          (or gnus-expert-user
6144              (gnus-yes-or-no-p
6145               (format
6146                "Do you really want to kill all groups on level %d? "
6147                level))))
6148     (let* ((prev gnus-newsrc-alist)
6149            (alist (cdr prev)))
6150       (while alist
6151         (if (= (gnus-info-level level) level)
6152             (setcdr prev (cdr alist))
6153           (setq prev alist))
6154         (setq alist (cdr alist)))
6155       (gnus-make-hashtable-from-newsrc-alist)
6156       (gnus-group-list-groups)))
6157    (t
6158     (error "Can't kill; illegal level: %d" level))))
6159
6160 (defun gnus-group-list-all-groups (&optional arg)
6161   "List all newsgroups with level ARG or lower.
6162 Default is gnus-level-unsubscribed, which lists all subscribed and most
6163 unsubscribed groups."
6164   (interactive "P")
6165   (gnus-group-list-groups (or arg gnus-level-unsubscribed) t))
6166
6167 ;; Redefine this to list ALL killed groups if prefix arg used.
6168 ;; Rewritten by engstrom@src.honeywell.com (Eric Engstrom).
6169 (defun gnus-group-list-killed (&optional arg)
6170   "List all killed newsgroups in the group buffer.
6171 If ARG is non-nil, list ALL killed groups known to Gnus.  This may
6172 entail asking the server for the groups."
6173   (interactive "P")
6174   ;; Find all possible killed newsgroups if arg.
6175   (when arg
6176     ;; First make sure active file has been read.
6177     (unless gnus-have-read-active-file
6178       (let ((gnus-read-active-file t))
6179         (gnus-read-active-file)))
6180     (or gnus-killed-hashtb (gnus-make-hashtable-from-killed))
6181     ;; Go through all newsgroups that are known to Gnus - enlarge kill list
6182     (mapatoms
6183      (lambda (sym)
6184        (let ((groups 0)
6185              (group (symbol-name sym)))
6186          (if (or (null group)
6187                  (gnus-gethash group gnus-killed-hashtb)
6188                  (gnus-gethash group gnus-newsrc-hashtb))
6189              ()
6190            (let ((do-sub (gnus-matches-options-n group)))
6191              (if (or (eq do-sub 'subscribe) (eq do-sub 'ignore))
6192                  ()
6193                (setq groups (1+ groups))
6194                (setq gnus-killed-list
6195                      (cons group gnus-killed-list))
6196                (gnus-sethash group group gnus-killed-hashtb))))))
6197      gnus-active-hashtb))
6198   (if (not gnus-killed-list)
6199       (gnus-message 6 "No killed groups")
6200     (let (gnus-group-list-mode)
6201       (funcall gnus-group-prepare-function
6202                gnus-level-killed t gnus-level-killed))
6203     (goto-char (point-min)))
6204   (gnus-group-position-point))
6205
6206 (defun gnus-group-list-zombies ()
6207   "List all zombie newsgroups in the group buffer."
6208   (interactive)
6209   (if (not gnus-zombie-list)
6210       (gnus-message 6 "No zombie groups")
6211     (let (gnus-group-list-mode)
6212       (funcall gnus-group-prepare-function
6213                gnus-level-zombie t gnus-level-zombie))
6214     (goto-char (point-min)))
6215   (gnus-group-position-point))
6216
6217 (defun gnus-group-list-active ()
6218   "List all groups that are available from the server(s)."
6219   (interactive)
6220   ;; First we make sure that we have really read the active file.
6221   (unless gnus-have-read-active-file
6222     (let ((gnus-read-active-file t))
6223       (gnus-read-active-file)))
6224   ;; Find all groups and sort them.
6225   (let ((groups
6226          (sort
6227           (let (list)
6228             (mapatoms
6229              (lambda (sym)
6230                (and (symbol-value sym)
6231                     (setq list (cons (symbol-name sym) list))))
6232              gnus-active-hashtb)
6233             list)
6234           'string<))
6235         (buffer-read-only nil))
6236     (erase-buffer)
6237     (while groups
6238       (gnus-group-insert-group-line-info (car groups))
6239       (setq groups (cdr groups)))
6240     (goto-char (point-min))))
6241
6242 (defun gnus-activate-all-groups (level)
6243   "Activate absolutely all groups."
6244   (interactive (list 7))
6245   (let ((gnus-activate-level level)
6246         (gnus-activate-foreign-newsgroups level))
6247     (gnus-group-get-new-news)))
6248
6249 (defun gnus-group-get-new-news (&optional arg)
6250   "Get newly arrived articles.
6251 If ARG is a number, it specifies which levels you are interested in
6252 re-scanning.  If ARG is non-nil and not a number, this will force
6253 \"hard\" re-reading of the active files from all servers."
6254   (interactive "P")
6255   (run-hooks 'gnus-get-new-news-hook)
6256   ;; We might read in new NoCeM messages here.
6257   (when (and gnus-use-nocem 
6258              (null arg))
6259     (gnus-nocem-scan-groups))
6260   ;; If ARG is not a number, then we read the active file.
6261   (when (and arg (not (numberp arg)))
6262     (let ((gnus-read-active-file t))
6263       (gnus-read-active-file))
6264     (setq arg nil))
6265
6266   (setq arg (gnus-group-default-level arg t))
6267   (if (and gnus-read-active-file (not arg))
6268       (progn
6269         (gnus-read-active-file)
6270         (gnus-get-unread-articles arg))
6271     (let ((gnus-read-active-file (if arg nil gnus-read-active-file)))
6272       (gnus-get-unread-articles arg)))
6273   (run-hooks 'gnus-after-getting-new-news-hook)
6274   (gnus-group-list-groups))
6275
6276 (defun gnus-group-get-new-news-this-group (&optional n)
6277   "Check for newly arrived news in the current group (and the N-1 next groups).
6278 The difference between N and the number of newsgroup checked is returned.
6279 If N is negative, this group and the N-1 previous groups will be checked."
6280   (interactive "P")
6281   (let* ((groups (gnus-group-process-prefix n))
6282          (ret (if (numberp n) (- n (length groups)) 0))
6283          group)
6284     (while groups
6285       (setq group (car groups)
6286             groups (cdr groups))
6287       (gnus-group-remove-mark group)
6288       (unless (gnus-get-new-news-in-group group)
6289         (ding)
6290         (gnus-message 3 "%s error: %s" group (gnus-status-message group))))
6291     (when gnus-goto-next-group-when-activating
6292       (gnus-group-next-unread-group 1 t))
6293     (gnus-summary-position-point)
6294     ret))
6295
6296 (defun gnus-get-new-news-in-group (group)
6297   (when (and group (gnus-activate-group group 'scan))
6298     (gnus-get-unread-articles-in-group
6299      (gnus-get-info group) (gnus-active group) t)
6300     (when (gnus-group-goto-group group)
6301       (gnus-group-update-group-line))
6302     t))
6303
6304 (defun gnus-group-fetch-faq (group &optional faq-dir)
6305   "Fetch the FAQ for the current group."
6306   (interactive
6307    (list
6308     (gnus-group-real-name (gnus-group-group-name))
6309     (cond (current-prefix-arg
6310            (completing-read
6311             "Faq dir: " (and (listp gnus-group-faq-directory)
6312                              gnus-group-faq-directory))))))
6313   (or faq-dir
6314       (setq faq-dir (if (listp gnus-group-faq-directory)
6315                         (car gnus-group-faq-directory)
6316                       gnus-group-faq-directory)))
6317   (or group (error "No group name given"))
6318   (let ((file (concat (file-name-as-directory faq-dir)
6319                       (gnus-group-real-name group))))
6320     (if (not (file-exists-p file))
6321         (error "No such file: %s" file)
6322       (find-file file))))
6323
6324 (defun gnus-group-describe-group (force &optional group)
6325   "Display a description of the current newsgroup."
6326   (interactive (list current-prefix-arg (gnus-group-group-name)))
6327   (and force (setq gnus-description-hashtb nil))
6328   (let ((method (gnus-find-method-for-group group))
6329         desc)
6330     (or group (error "No group name given"))
6331     (and (or (and gnus-description-hashtb
6332                   ;; We check whether this group's method has been
6333                   ;; queried for a description file.
6334                   (gnus-gethash
6335                    (gnus-group-prefixed-name "" method)
6336                    gnus-description-hashtb))
6337              (setq desc (gnus-group-get-description group))
6338              (gnus-read-descriptions-file method))
6339          (message
6340           (or desc (gnus-gethash group gnus-description-hashtb)
6341               "No description available")))))
6342
6343 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6344 (defun gnus-group-describe-all-groups (&optional force)
6345   "Pop up a buffer with descriptions of all newsgroups."
6346   (interactive "P")
6347   (and force (setq gnus-description-hashtb nil))
6348   (if (not (or gnus-description-hashtb
6349                (gnus-read-all-descriptions-files)))
6350       (error "Couldn't request descriptions file"))
6351   (let ((buffer-read-only nil)
6352         b)
6353     (erase-buffer)
6354     (mapatoms
6355      (lambda (group)
6356        (setq b (point))
6357        (insert (format "      *: %-20s %s\n" (symbol-name group)
6358                        (symbol-value group)))
6359        (add-text-properties
6360         b (1+ b) (list 'gnus-group group
6361                        'gnus-unread t 'gnus-marked nil
6362                        'gnus-level (1+ gnus-level-subscribed))))
6363      gnus-description-hashtb)
6364     (goto-char (point-min))
6365     (gnus-group-position-point)))
6366
6367 ;; Suggested by by Daniel Quinlan <quinlan@best.com>.
6368 (defun gnus-group-apropos (regexp &optional search-description)
6369   "List all newsgroups that have names that match a regexp."
6370   (interactive "sGnus apropos (regexp): ")
6371   (let ((prev "")
6372         (obuf (current-buffer))
6373         groups des)
6374     ;; Go through all newsgroups that are known to Gnus.
6375     (mapatoms
6376      (lambda (group)
6377        (and (symbol-name group)
6378             (string-match regexp (symbol-name group))
6379             (setq groups (cons (symbol-name group) groups))))
6380      gnus-active-hashtb)
6381     ;; Go through all descriptions that are known to Gnus.
6382     (if search-description
6383         (mapatoms
6384          (lambda (group)
6385            (and (string-match regexp (symbol-value group))
6386                 (gnus-active (symbol-name group))
6387                 (setq groups (cons (symbol-name group) groups))))
6388          gnus-description-hashtb))
6389     (if (not groups)
6390         (gnus-message 3 "No groups matched \"%s\"." regexp)
6391       ;; Print out all the groups.
6392       (save-excursion
6393         (pop-to-buffer "*Gnus Help*")
6394         (buffer-disable-undo (current-buffer))
6395         (erase-buffer)
6396         (setq groups (sort groups 'string<))
6397         (while groups
6398           ;; Groups may be entered twice into the list of groups.
6399           (if (not (string= (car groups) prev))
6400               (progn
6401                 (insert (setq prev (car groups)) "\n")
6402                 (if (and gnus-description-hashtb
6403                          (setq des (gnus-gethash (car groups)
6404                                                  gnus-description-hashtb)))
6405                     (insert "  " des "\n"))))
6406           (setq groups (cdr groups)))
6407         (goto-char (point-min))))
6408     (pop-to-buffer obuf)))
6409
6410 (defun gnus-group-description-apropos (regexp)
6411   "List all newsgroups that have names or descriptions that match a regexp."
6412   (interactive "sGnus description apropos (regexp): ")
6413   (if (not (or gnus-description-hashtb
6414                (gnus-read-all-descriptions-files)))
6415       (error "Couldn't request descriptions file"))
6416   (gnus-group-apropos regexp t))
6417
6418 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
6419 (defun gnus-group-list-matching (level regexp &optional all lowest)
6420   "List all groups with unread articles that match REGEXP.
6421 If the prefix LEVEL is non-nil, it should be a number that says which
6422 level to cut off listing groups.
6423 If ALL, also list groups with no unread articles.
6424 If LOWEST, don't list groups with level lower than LOWEST."
6425   (interactive "P\nsList newsgroups matching: ")
6426   (gnus-group-prepare-flat (or level gnus-level-subscribed)
6427                            all (or lowest 1) regexp)
6428   (goto-char (point-min))
6429   (gnus-group-position-point))
6430
6431 (defun gnus-group-list-all-matching (level regexp &optional lowest)
6432   "List all groups that match REGEXP.
6433 If the prefix LEVEL is non-nil, it should be a number that says which
6434 level to cut off listing groups.
6435 If LOWEST, don't list groups with level lower than LOWEST."
6436   (interactive "P\nsList newsgroups matching: ")
6437   (gnus-group-list-matching (or level gnus-level-killed) regexp t lowest))
6438
6439 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
6440 (defun gnus-group-save-newsrc (&optional force)
6441   "Save the Gnus startup files.
6442 If FORCE, force saving whether it is necessary or not."
6443   (interactive "P")
6444   (gnus-save-newsrc-file force))
6445
6446 (defun gnus-group-restart (&optional arg)
6447   "Force Gnus to read the .newsrc file."
6448   (interactive "P")
6449   (when (gnus-yes-or-no-p
6450          (format "Are you sure you want to read %s? "
6451                  gnus-current-startup-file))
6452     (gnus-save-newsrc-file)
6453     (gnus-setup-news 'force)
6454     (gnus-group-list-groups arg)))
6455
6456 (defun gnus-group-read-init-file ()
6457   "Read the Gnus elisp init file."
6458   (interactive)
6459   (gnus-read-init-file))
6460
6461 (defun gnus-group-check-bogus-groups (&optional silent)
6462   "Check bogus newsgroups.
6463 If given a prefix, don't ask for confirmation before removing a bogus
6464 group."
6465   (interactive "P")
6466   (gnus-check-bogus-newsgroups (and (not silent) (not gnus-expert-user)))
6467   (gnus-group-list-groups))
6468
6469 (defun gnus-group-edit-global-kill (&optional article group)
6470   "Edit the global kill file.
6471 If GROUP, edit that local kill file instead."
6472   (interactive "P")
6473   (setq gnus-current-kill-article article)
6474   (gnus-kill-file-edit-file group)
6475   (gnus-message
6476    6
6477    (substitute-command-keys
6478     (format "Editing a %s kill file (Type \\[gnus-kill-file-exit] to exit)"
6479             (if group "local" "global")))))
6480
6481 (defun gnus-group-edit-local-kill (article group)
6482   "Edit a local kill file."
6483   (interactive (list nil (gnus-group-group-name)))
6484   (gnus-group-edit-global-kill article group))
6485
6486 (defun gnus-group-force-update ()
6487   "Update `.newsrc' file."
6488   (interactive)
6489   (gnus-save-newsrc-file))
6490
6491 (defun gnus-group-suspend ()
6492   "Suspend the current Gnus session.
6493 In fact, cleanup buffers except for group mode buffer.
6494 The hook gnus-suspend-gnus-hook is called before actually suspending."
6495   (interactive)
6496   (run-hooks 'gnus-suspend-gnus-hook)
6497   ;; Kill Gnus buffers except for group mode buffer.
6498   (let ((group-buf (get-buffer gnus-group-buffer)))
6499     ;; Do this on a separate list in case the user does a ^G before we finish
6500     (let ((gnus-buffer-list
6501            (delq group-buf (delq gnus-dribble-buffer
6502                                  (append gnus-buffer-list nil)))))
6503       (while gnus-buffer-list
6504         (gnus-kill-buffer (car gnus-buffer-list))
6505         (setq gnus-buffer-list (cdr gnus-buffer-list))))
6506     (if group-buf
6507         (progn
6508           (setq gnus-buffer-list (list group-buf))
6509           (bury-buffer group-buf)
6510           (delete-windows-on group-buf t)))))
6511
6512 (defun gnus-group-clear-dribble ()
6513   "Clear all information from the dribble buffer."
6514   (interactive)
6515   (gnus-dribble-clear)
6516   (gnus-message 7 "Cleared dribble buffer"))
6517
6518 (defun gnus-group-exit ()
6519   "Quit reading news after updating .newsrc.eld and .newsrc.
6520 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6521   (interactive)
6522   (when 
6523       (or noninteractive                ;For gnus-batch-kill
6524           (not (gnus-server-opened gnus-select-method)) ;NNTP connection closed
6525           (not gnus-interactive-exit)   ;Without confirmation
6526           gnus-expert-user
6527           (gnus-y-or-n-p "Are you sure you want to quit reading news? "))
6528     (run-hooks 'gnus-exit-gnus-hook)
6529     ;; Close down GroupLens.
6530     (when gnus-use-grouplens
6531       (bbb-logout))
6532     ;; Offer to save data from non-quitted summary buffers.
6533     (gnus-offer-save-summaries)
6534     ;; Save the newsrc file(s).
6535     (gnus-save-newsrc-file)
6536     ;; Kill-em-all.
6537     (gnus-close-backends)
6538     ;; Shut down the cache.
6539     (when gnus-use-cache
6540       (gnus-cache-close))
6541     ;; Reset everything.
6542     (gnus-clear-system)
6543     ;; Allow the user to do things after cleaning up.
6544     (run-hooks 'gnus-after-exiting-gnus-hook)))
6545
6546 (defun gnus-close-backends ()
6547   ;; Send a close request to all backends that support such a request.
6548   (let ((methods gnus-valid-select-methods)
6549         func)
6550     (while methods
6551       (if (fboundp (setq func (intern (concat (caar methods)
6552                                               "-request-close"))))
6553           (funcall func))
6554       (setq methods (cdr methods)))))
6555
6556 (defun gnus-group-quit ()
6557   "Quit reading news without updating .newsrc.eld or .newsrc.
6558 The hook `gnus-exit-gnus-hook' is called before actually exiting."
6559   (interactive)
6560   (when (or noninteractive              ;For gnus-batch-kill
6561             (zerop (buffer-size))
6562             (not (gnus-server-opened gnus-select-method))
6563             gnus-expert-user
6564             (not gnus-current-startup-file)
6565             (gnus-yes-or-no-p
6566              (format "Quit reading news without saving %s? "
6567                      (file-name-nondirectory gnus-current-startup-file))))
6568     (run-hooks 'gnus-exit-gnus-hook)
6569     (if gnus-use-full-window
6570         (delete-other-windows)
6571       (gnus-remove-some-windows))
6572     (gnus-dribble-save)
6573     (gnus-close-backends)
6574     ;; Shut down the cache.
6575     (when gnus-use-cache
6576       (gnus-cache-close))
6577     (gnus-clear-system)
6578     ;; Allow the user to do things after cleaning up.
6579     (run-hooks 'gnus-after-exiting-gnus-hook)))
6580
6581 (defun gnus-offer-save-summaries ()
6582   "Offer to save all active summary buffers."
6583   (save-excursion
6584     (let ((buflist (buffer-list))
6585           buffers bufname)
6586       ;; Go through all buffers and find all summaries.
6587       (while buflist
6588         (and (setq bufname (buffer-name (car buflist)))
6589              (string-match "Summary" bufname)
6590              (save-excursion
6591                (set-buffer bufname)
6592                ;; We check that this is, indeed, a summary buffer.
6593                (and (eq major-mode 'gnus-summary-mode)
6594                     ;; Also make sure this isn't bogus.
6595                     gnus-newsgroup-prepared))
6596              (push bufname buffers))
6597         (setq buflist (cdr buflist)))
6598       ;; Go through all these summary buffers and offer to save them.
6599       (when buffers
6600         (map-y-or-n-p
6601          "Update summary buffer %s? "
6602          (lambda (buf) (set-buffer buf) (gnus-summary-exit))
6603          buffers)))))
6604
6605 (defun gnus-group-describe-briefly ()
6606   "Give a one line description of the group mode commands."
6607   (interactive)
6608   (gnus-message 7 (substitute-command-keys "\\<gnus-group-mode-map>\\[gnus-group-read-group]:Select  \\[gnus-group-next-unread-group]:Forward  \\[gnus-group-prev-unread-group]:Backward  \\[gnus-group-exit]:Exit  \\[gnus-info-find-node]:Run Info  \\[gnus-group-describe-briefly]:This help")))
6609
6610 (defun gnus-group-browse-foreign-server (method)
6611   "Browse a foreign news server.
6612 If called interactively, this function will ask for a select method
6613  (nntp, nnspool, etc.) and a server address (eg. nntp.some.where).
6614 If not, METHOD should be a list where the first element is the method
6615 and the second element is the address."
6616   (interactive
6617    (list (let ((how (completing-read
6618                      "Which backend: "
6619                      (append gnus-valid-select-methods gnus-server-alist)
6620                      nil t (cons "nntp" 0))))
6621            ;; We either got a backend name or a virtual server name.
6622            ;; If the first, we also need an address.
6623            (if (assoc how gnus-valid-select-methods)
6624                (list (intern how)
6625                      ;; Suggested by mapjph@bath.ac.uk.
6626                      (completing-read
6627                       "Address: "
6628                       (mapcar (lambda (server) (list server))
6629                               gnus-secondary-servers)))
6630              ;; We got a server name, so we find the method.
6631              (gnus-server-to-method how)))))
6632   (gnus-browse-foreign-server method))
6633
6634 \f
6635 ;;;
6636 ;;; Gnus summary mode
6637 ;;;
6638
6639 (defvar gnus-summary-mode-map nil)
6640
6641 (put 'gnus-summary-mode 'mode-class 'special)
6642
6643 (unless gnus-summary-mode-map
6644   (setq gnus-summary-mode-map (make-keymap))
6645   (suppress-keymap gnus-summary-mode-map)
6646
6647   ;; Non-orthogonal keys
6648
6649   (gnus-define-keys gnus-summary-mode-map
6650     " " gnus-summary-next-page
6651     "\177" gnus-summary-prev-page
6652     [delete] gnus-summary-prev-page
6653     "\r" gnus-summary-scroll-up
6654     "n" gnus-summary-next-unread-article
6655     "p" gnus-summary-prev-unread-article
6656     "N" gnus-summary-next-article
6657     "P" gnus-summary-prev-article
6658     "\M-\C-n" gnus-summary-next-same-subject
6659     "\M-\C-p" gnus-summary-prev-same-subject
6660     "\M-n" gnus-summary-next-unread-subject
6661     "\M-p" gnus-summary-prev-unread-subject
6662     "." gnus-summary-first-unread-article
6663     "," gnus-summary-best-unread-article
6664     "\M-s" gnus-summary-search-article-forward
6665     "\M-r" gnus-summary-search-article-backward
6666     "<" gnus-summary-beginning-of-article
6667     ">" gnus-summary-end-of-article
6668     "j" gnus-summary-goto-article
6669     "^" gnus-summary-refer-parent-article
6670     "\M-^" gnus-summary-refer-article
6671     "u" gnus-summary-tick-article-forward
6672     "!" gnus-summary-tick-article-forward
6673     "U" gnus-summary-tick-article-backward
6674     "d" gnus-summary-mark-as-read-forward
6675     "D" gnus-summary-mark-as-read-backward
6676     "E" gnus-summary-mark-as-expirable
6677     "\M-u" gnus-summary-clear-mark-forward
6678     "\M-U" gnus-summary-clear-mark-backward
6679     "k" gnus-summary-kill-same-subject-and-select
6680     "\C-k" gnus-summary-kill-same-subject
6681     "\M-\C-k" gnus-summary-kill-thread
6682     "\M-\C-l" gnus-summary-lower-thread
6683     "e" gnus-summary-edit-article
6684     "#" gnus-summary-mark-as-processable
6685     "\M-#" gnus-summary-unmark-as-processable
6686     "\M-\C-t" gnus-summary-toggle-threads
6687     "\M-\C-s" gnus-summary-show-thread
6688     "\M-\C-h" gnus-summary-hide-thread
6689     "\M-\C-f" gnus-summary-next-thread
6690     "\M-\C-b" gnus-summary-prev-thread
6691     "\M-\C-u" gnus-summary-up-thread
6692     "\M-\C-d" gnus-summary-down-thread
6693     "&" gnus-summary-execute-command
6694     "c" gnus-summary-catchup-and-exit
6695     "\C-w" gnus-summary-mark-region-as-read
6696     "\C-t" gnus-summary-toggle-truncation
6697     "?" gnus-summary-mark-as-dormant
6698     "\C-c\M-\C-s" gnus-summary-limit-include-expunged
6699     "\C-c\C-s\C-n" gnus-summary-sort-by-number
6700     "\C-c\C-s\C-a" gnus-summary-sort-by-author
6701     "\C-c\C-s\C-s" gnus-summary-sort-by-subject
6702     "\C-c\C-s\C-d" gnus-summary-sort-by-date
6703     "\C-c\C-s\C-i" gnus-summary-sort-by-score
6704     "=" gnus-summary-expand-window
6705     "\C-x\C-s" gnus-summary-reselect-current-group
6706     "\M-g" gnus-summary-rescan-group
6707     "w" gnus-summary-stop-page-breaking
6708     "\C-c\C-r" gnus-summary-caesar-message
6709     "\M-t" gnus-summary-toggle-mime
6710     "f" gnus-summary-followup
6711     "F" gnus-summary-followup-with-original
6712     "C" gnus-summary-cancel-article
6713     "r" gnus-summary-reply
6714     "R" gnus-summary-reply-with-original
6715     "\C-c\C-f" gnus-summary-mail-forward
6716     "o" gnus-summary-save-article
6717     "\C-o" gnus-summary-save-article-mail
6718     "|" gnus-summary-pipe-output
6719     "\M-k" gnus-summary-edit-local-kill
6720     "\M-K" gnus-summary-edit-global-kill
6721     "V" gnus-version
6722     "\C-c\C-d" gnus-summary-describe-group
6723     "q" gnus-summary-exit
6724     "Q" gnus-summary-exit-no-update
6725     "\C-c\C-i" gnus-info-find-node
6726     gnus-mouse-2 gnus-mouse-pick-article
6727     "m" gnus-summary-mail-other-window
6728     "a" gnus-summary-post-news
6729     "x" gnus-summary-limit-to-unread
6730     "s" gnus-summary-isearch-article
6731     "t" gnus-article-hide-headers
6732     "g" gnus-summary-show-article
6733     "l" gnus-summary-goto-last-article
6734     "\C-c\C-v\C-v" gnus-uu-decode-uu-view
6735     "\C-d" gnus-summary-enter-digest-group
6736     "\C-c\C-b" gnus-bug
6737     "*" gnus-cache-enter-article
6738     "\M-*" gnus-cache-remove-article
6739     "\M-&" gnus-summary-universal-argument
6740     "\C-l" gnus-recenter
6741     "I" gnus-summary-increase-score
6742     "L" gnus-summary-lower-score
6743
6744     "V" gnus-summary-score-map
6745     "X" gnus-uu-extract-map
6746     "S" gnus-summary-send-map)
6747
6748   ;; Sort of orthogonal keymap
6749   (gnus-define-keys (gnus-summary-mark-map "M" gnus-summary-mode-map)
6750     "t" gnus-summary-tick-article-forward
6751     "!" gnus-summary-tick-article-forward
6752     "d" gnus-summary-mark-as-read-forward
6753     "r" gnus-summary-mark-as-read-forward
6754     "c" gnus-summary-clear-mark-forward
6755     " " gnus-summary-clear-mark-forward
6756     "e" gnus-summary-mark-as-expirable
6757     "x" gnus-summary-mark-as-expirable
6758     "?" gnus-summary-mark-as-dormant
6759     "b" gnus-summary-set-bookmark
6760     "B" gnus-summary-remove-bookmark
6761     "#" gnus-summary-mark-as-processable
6762     "\M-#" gnus-summary-unmark-as-processable
6763     "S" gnus-summary-limit-include-expunged
6764     "C" gnus-summary-catchup
6765     "H" gnus-summary-catchup-to-here
6766     "\C-c" gnus-summary-catchup-all
6767     "k" gnus-summary-kill-same-subject-and-select
6768     "K" gnus-summary-kill-same-subject
6769     "P" gnus-uu-mark-map)
6770
6771   (gnus-define-keys (gnus-summary-mscore-map "V" gnus-summary-mode-map)
6772     "c" gnus-summary-clear-above
6773     "u" gnus-summary-tick-above
6774     "m" gnus-summary-mark-above
6775     "k" gnus-summary-kill-below)
6776
6777   (gnus-define-keys (gnus-summary-limit-map "/" gnus-summary-mode-map)
6778     "/" gnus-summary-limit-to-subject
6779     "n" gnus-summary-limit-to-articles
6780     "w" gnus-summary-pop-limit
6781     "s" gnus-summary-limit-to-subject
6782     "a" gnus-summary-limit-to-author
6783     "u" gnus-summary-limit-to-unread
6784     "m" gnus-summary-limit-to-marks
6785     "v" gnus-summary-limit-to-score
6786     "D" gnus-summary-limit-include-dormant
6787     "d" gnus-summary-limit-exclude-dormant
6788     ;;  "t" gnus-summary-limit-exclude-thread
6789     "E" gnus-summary-limit-include-expunged
6790     "c" gnus-summary-limit-exclude-childless-dormant
6791     "C" gnus-summary-limit-mark-excluded-as-read)
6792
6793   (gnus-define-keys (gnus-summary-goto-map "G" gnus-summary-mode-map)
6794     "n" gnus-summary-next-unread-article
6795     "p" gnus-summary-prev-unread-article
6796     "N" gnus-summary-next-article
6797     "P" gnus-summary-prev-article
6798     "\C-n" gnus-summary-next-same-subject
6799     "\C-p" gnus-summary-prev-same-subject
6800     "\M-n" gnus-summary-next-unread-subject
6801     "\M-p" gnus-summary-prev-unread-subject
6802     "f" gnus-summary-first-unread-article
6803     "b" gnus-summary-best-unread-article
6804     "g" gnus-summary-goto-subject
6805     "l" gnus-summary-goto-last-article
6806     "p" gnus-summary-pop-article)
6807
6808   (gnus-define-keys (gnus-summary-thread-map "T" gnus-summary-mode-map)
6809     "k" gnus-summary-kill-thread
6810     "l" gnus-summary-lower-thread
6811     "i" gnus-summary-raise-thread
6812     "T" gnus-summary-toggle-threads
6813     "t" gnus-summary-rethread-current
6814     "^" gnus-summary-reparent-thread
6815     "s" gnus-summary-show-thread
6816     "S" gnus-summary-show-all-threads
6817     "h" gnus-summary-hide-thread
6818     "H" gnus-summary-hide-all-threads
6819     "n" gnus-summary-next-thread
6820     "p" gnus-summary-prev-thread
6821     "u" gnus-summary-up-thread
6822     "o" gnus-summary-top-thread
6823     "d" gnus-summary-down-thread
6824     "#" gnus-uu-mark-thread
6825     "\M-#" gnus-uu-unmark-thread)
6826
6827   (gnus-define-keys (gnus-summary-exit-map "Z" gnus-summary-mode-map)
6828     "c" gnus-summary-catchup-and-exit
6829     "C" gnus-summary-catchup-all-and-exit
6830     "E" gnus-summary-exit-no-update
6831     "Q" gnus-summary-exit
6832     "Z" gnus-summary-exit
6833     "n" gnus-summary-catchup-and-goto-next-group
6834     "R" gnus-summary-reselect-current-group
6835     "G" gnus-summary-rescan-group
6836     "N" gnus-summary-next-group
6837     "P" gnus-summary-prev-group)
6838
6839   (gnus-define-keys (gnus-summary-article-map "A" gnus-summary-mode-map)
6840     " " gnus-summary-next-page
6841     "n" gnus-summary-next-page
6842     "\177" gnus-summary-prev-page
6843     [delete] gnus-summary-prev-page
6844     "p" gnus-summary-prev-page
6845     "\r" gnus-summary-scroll-up
6846     "<" gnus-summary-beginning-of-article
6847     ">" gnus-summary-end-of-article
6848     "b" gnus-summary-beginning-of-article
6849     "e" gnus-summary-end-of-article
6850     "^" gnus-summary-refer-parent-article
6851     "r" gnus-summary-refer-parent-article
6852     "R" gnus-summary-refer-references
6853     "g" gnus-summary-show-article
6854     "s" gnus-summary-isearch-article)
6855
6856   (gnus-define-keys (gnus-summary-wash-map "W" gnus-summary-mode-map)
6857     "b" gnus-article-add-buttons
6858     "B" gnus-article-add-buttons-to-head
6859     "o" gnus-article-treat-overstrike
6860     ;;  "w" gnus-article-word-wrap
6861     "w" gnus-article-fill-cited-article
6862     "c" gnus-article-remove-cr
6863     "L" gnus-article-remove-trailing-blank-lines
6864     "q" gnus-article-de-quoted-unreadable
6865     "f" gnus-article-display-x-face
6866     "l" gnus-summary-stop-page-breaking
6867     "r" gnus-summary-caesar-message
6868     "t" gnus-article-hide-headers
6869     "v" gnus-summary-verbose-headers
6870     "m" gnus-summary-toggle-mime)
6871
6872   (gnus-define-keys (gnus-summary-wash-hide-map "W" gnus-summary-wash-map)
6873     "a" gnus-article-hide
6874     "h" gnus-article-hide-headers
6875     "b" gnus-article-hide-boring-headers
6876     "s" gnus-article-hide-signature
6877     "c" gnus-article-hide-citation
6878     "p" gnus-article-hide-pgp
6879     "\C-c" gnus-article-hide-citation-maybe)
6880
6881   (gnus-define-keys (gnus-summary-wash-highlight-map "H" gnus-summary-wash-map)
6882     "a" gnus-article-highlight
6883     "h" gnus-article-highlight-headers
6884     "c" gnus-article-highlight-citation
6885     "s" gnus-article-highlight-signature)
6886
6887   (gnus-define-keys (gnus-summary-wash-time-map "T" gnus-summary-wash-map)
6888     "z" gnus-article-date-ut
6889     "u" gnus-article-date-ut
6890     "l" gnus-article-date-local
6891     "e" gnus-article-date-lapsed
6892     "o" gnus-article-date-original)
6893
6894   (gnus-define-keys (gnus-summary-help-map "H" gnus-summary-mode-map)
6895     "v" gnus-version
6896     "f" gnus-summary-fetch-faq
6897     "d" gnus-summary-describe-group
6898     "h" gnus-summary-describe-briefly
6899     "i" gnus-info-find-node)
6900
6901   (gnus-define-keys (gnus-summary-backend-map "B" gnus-summary-mode-map)
6902     "e" gnus-summary-expire-articles
6903     "\M-\C-e" gnus-summary-expire-articles-now
6904     "\177" gnus-summary-delete-article
6905     [delete] gnus-summary-delete-article
6906     "m" gnus-summary-move-article
6907     "r" gnus-summary-respool-article
6908     "w" gnus-summary-edit-article
6909     "c" gnus-summary-copy-article
6910     "B" gnus-summary-crosspost-article
6911     "q" gnus-summary-respool-query
6912     "i" gnus-summary-import-article)
6913
6914   (gnus-define-keys (gnus-summary-save-map "O" gnus-summary-mode-map)
6915     "o" gnus-summary-save-article
6916     "m" gnus-summary-save-article-mail
6917     "r" gnus-summary-save-article-rmail
6918     "f" gnus-summary-save-article-file
6919     "b" gnus-summary-save-article-body-file
6920     "h" gnus-summary-save-article-folder
6921     "v" gnus-summary-save-article-vm
6922     "p" gnus-summary-pipe-output
6923     "s" gnus-soup-add-article)
6924   )
6925
6926 \f
6927
6928 (defun gnus-summary-mode (&optional group)
6929   "Major mode for reading articles.
6930
6931 All normal editing commands are switched off.
6932 \\<gnus-summary-mode-map>
6933 Each line in this buffer represents one article.  To read an
6934 article, you can, for instance, type `\\[gnus-summary-next-page]'.  To move forwards
6935 and backwards while displaying articles, type `\\[gnus-summary-next-unread-article]' and `\\[gnus-summary-prev-unread-article]',
6936 respectively.
6937
6938 You can also post articles and send mail from this buffer.  To
6939 follow up an article, type `\\[gnus-summary-followup]'.  To mail a reply to the author
6940 of an article, type `\\[gnus-summary-reply]'.
6941
6942 There are approx. one gazillion commands you can execute in this
6943 buffer; read the info pages for more information (`\\[gnus-info-find-node]').
6944
6945 The following commands are available:
6946
6947 \\{gnus-summary-mode-map}"
6948   (interactive)
6949   (when (and menu-bar-mode
6950              (gnus-visual-p 'summary-menu 'menu))
6951     (gnus-summary-make-menu-bar))
6952   (kill-all-local-variables)
6953   (let ((locals gnus-summary-local-variables))
6954     (while locals
6955       (if (consp (car locals))
6956           (progn
6957             (make-local-variable (caar locals))
6958             (set (caar locals) (eval (cdar locals))))
6959         (make-local-variable (car locals))
6960         (set (car locals) nil))
6961       (setq locals (cdr locals))))
6962   (gnus-make-thread-indent-array)
6963   (gnus-simplify-mode-line)
6964   (setq major-mode 'gnus-summary-mode)
6965   (setq mode-name "Summary")
6966   (make-local-variable 'minor-mode-alist)
6967   (use-local-map gnus-summary-mode-map)
6968   (buffer-disable-undo (current-buffer))
6969   (setq buffer-read-only t)             ;Disable modification
6970   (setq truncate-lines t)
6971   (setq selective-display t)
6972   (setq selective-display-ellipses t)   ;Display `...'
6973   (setq buffer-display-table gnus-summary-display-table)
6974   (setq gnus-newsgroup-name group)
6975   (run-hooks 'gnus-summary-mode-hook))
6976
6977 (defun gnus-summary-make-display-table ()
6978   ;; Change the display table.  Odd characters have a tendency to mess
6979   ;; up nicely formatted displays - we make all possible glyphs
6980   ;; display only a single character.
6981
6982   ;; We start from the standard display table, if any.
6983   (setq gnus-summary-display-table
6984         (or (copy-sequence standard-display-table)
6985             (make-display-table)))
6986   ;; Nix out all the control chars...
6987   (let ((i 32))
6988     (while (>= (setq i (1- i)) 0)
6989       (aset gnus-summary-display-table i [??])))
6990   ;; ... but not newline and cr, of course. (cr is necessary for the
6991   ;; selective display).
6992   (aset gnus-summary-display-table ?\n nil)
6993   (aset gnus-summary-display-table ?\r nil)
6994   ;; We nix out any glyphs over 126 that are not set already.
6995   (let ((i 256))
6996     (while (>= (setq i (1- i)) 127)
6997       ;; Only modify if the entry is nil.
6998       (or (aref gnus-summary-display-table i)
6999           (aset gnus-summary-display-table i [??])))))
7000
7001 (defun gnus-summary-clear-local-variables ()
7002   (let ((locals gnus-summary-local-variables))
7003     (while locals
7004       (if (consp (car locals))
7005           (and (vectorp (caar locals))
7006                (set (caar locals) nil))
7007         (and (vectorp (car locals))
7008              (set (car locals) nil)))
7009       (setq locals (cdr locals)))))
7010
7011 ;; Summary data functions.
7012
7013 (defmacro gnus-data-number (data)
7014   `(car ,data))
7015
7016 (defmacro gnus-data-set-number (data number)
7017   `(setcar ,data ,number))
7018
7019 (defmacro gnus-data-mark (data)
7020   `(nth 1 ,data))
7021
7022 (defmacro gnus-data-set-mark (data mark)
7023   `(setcar (nthcdr 1 ,data) ,mark))
7024
7025 (defmacro gnus-data-pos (data)
7026   `(nth 2 ,data))
7027
7028 (defmacro gnus-data-set-pos (data pos)
7029   `(setcar (nthcdr 2 ,data) ,pos))
7030
7031 (defmacro gnus-data-header (data)
7032   `(nth 3 ,data))
7033
7034 (defmacro gnus-data-level (data)
7035   `(nth 4 ,data))
7036
7037 (defmacro gnus-data-unread-p (data)
7038   `(= (nth 1 ,data) gnus-unread-mark))
7039
7040 (defmacro gnus-data-pseudo-p (data)
7041   `(consp (nth 3 ,data)))
7042
7043 (defmacro gnus-data-find (number)
7044   `(assq ,number gnus-newsgroup-data))
7045
7046 (defmacro gnus-data-find-list (number &optional data)
7047   `(let ((bdata ,(or data 'gnus-newsgroup-data)))
7048      (memq (assq ,number bdata)
7049            bdata)))
7050
7051 (defmacro gnus-data-make (number mark pos header level)
7052   `(list ,number ,mark ,pos ,header ,level))
7053
7054 (defun gnus-data-enter (after-article number mark pos header level offset)
7055   (let ((data (gnus-data-find-list after-article)))
7056     (or data (error "No such article: %d" after-article))
7057     (setcdr data (cons (gnus-data-make number mark pos header level)
7058                        (cdr data)))
7059     (setq gnus-newsgroup-data-reverse nil)
7060     (gnus-data-update-list (cddr data) offset)))
7061
7062 (defun gnus-data-enter-list (after-article list &optional offset)
7063   (when list
7064     (let ((data (and after-article (gnus-data-find-list after-article)))
7065           (ilist list))
7066       (or data (not after-article) (error "No such article: %d" after-article))
7067       ;; Find the last element in the list to be spliced into the main
7068       ;; list.
7069       (while (cdr list)
7070         (setq list (cdr list)))
7071       (if (not data)
7072           (progn
7073             (setcdr list gnus-newsgroup-data)
7074             (setq gnus-newsgroup-data ilist)
7075             (and offset (gnus-data-update-list (cdr list) offset)))
7076         (setcdr list (cdr data))
7077         (setcdr data ilist)
7078         (and offset (gnus-data-update-list (cdr data) offset)))
7079       (setq gnus-newsgroup-data-reverse nil))))
7080
7081 (defun gnus-data-remove (article &optional offset)
7082   (let ((data gnus-newsgroup-data))
7083     (if (= (gnus-data-number (car data)) article)
7084         (setq gnus-newsgroup-data (cdr gnus-newsgroup-data)
7085               gnus-newsgroup-data-reverse nil)
7086       (while (cdr data)
7087         (and (= (gnus-data-number (cadr data)) article)
7088              (progn
7089                (setcdr data (cddr data))
7090                (and offset (gnus-data-update-list (cdr data) offset))
7091                (setq data nil
7092                      gnus-newsgroup-data-reverse nil)))
7093         (setq data (cdr data))))))
7094
7095 (defmacro gnus-data-list (backward)
7096   `(if ,backward
7097        (or gnus-newsgroup-data-reverse
7098            (setq gnus-newsgroup-data-reverse
7099                  (reverse gnus-newsgroup-data)))
7100      gnus-newsgroup-data))
7101
7102 (defun gnus-data-update-list (data offset)
7103   "Add OFFSET to the POS of all data entries in DATA."
7104   (while data
7105     (setcar (nthcdr 2 (car data)) (+ offset (nth 2 (car data))))
7106     (setq data (cdr data))))
7107
7108 (defun gnus-data-compute-positions ()
7109   "Compute the positions of all articles."
7110   (let ((data gnus-newsgroup-data)
7111         pos)
7112     (while data
7113       (when (setq pos (text-property-any
7114                        (point-min) (point-max)
7115                        'gnus-number (gnus-data-number (car data))))
7116         (gnus-data-set-pos (car data) (+ pos 3)))
7117       (setq data (cdr data)))))
7118
7119 (defun gnus-summary-article-pseudo-p (article)
7120   "Say whether this article is a pseudo article or not."
7121   (not (vectorp (gnus-data-header (gnus-data-find article)))))
7122
7123 (defun gnus-article-parent-p (number)
7124   "Say whether this article is a parent or not."
7125   (let ((data (gnus-data-find-list number)))
7126     (and (cdr data)                     ; There has to be an article after...
7127          (< (gnus-data-level (car data)) ; And it has to have a higher level.
7128             (gnus-data-level (nth 1 data))))))
7129
7130 (defun gnus-article-children (number)
7131   "Return a list of all children to NUMBER."
7132   (let* ((data (gnus-data-find-list number))
7133          (level (gnus-data-level (car data)))
7134          children)
7135     (setq data (cdr data))
7136     (while (and data            
7137                 (= (gnus-data-level (car data)) (1+ level)))
7138       (push (gnus-data-number (car data)) children)
7139       (setq data (cdr data)))
7140     children))
7141
7142 (defmacro gnus-summary-skip-intangible ()
7143   "If the current article is intangible, then jump to a different article."
7144   '(let ((to (get-text-property (point) 'gnus-intangible)))
7145     (and to (gnus-summary-goto-subject to))))
7146
7147 (defmacro gnus-summary-article-intangible-p ()
7148   "Say whether this article is intangible or not."
7149   '(get-text-property (point) 'gnus-intangible))
7150
7151 ;; Some summary mode macros.
7152
7153 (defmacro gnus-summary-article-number ()
7154   "The article number of the article on the current line.
7155 If there isn's an article number here, then we return the current
7156 article number."
7157   '(progn
7158      (gnus-summary-skip-intangible)
7159      (or (get-text-property (point) 'gnus-number)
7160          (gnus-summary-last-subject))))
7161
7162 (defmacro gnus-summary-article-header (&optional number)
7163   `(gnus-data-header (gnus-data-find
7164                       ,(or number '(gnus-summary-article-number)))))
7165
7166 (defmacro gnus-summary-thread-level (&optional number)
7167   `(if (and (eq gnus-summary-make-false-root 'dummy)
7168             (get-text-property (point) 'gnus-intangible))
7169        0
7170      (gnus-data-level (gnus-data-find
7171                        ,(or number '(gnus-summary-article-number))))))
7172
7173 (defmacro gnus-summary-article-mark (&optional number)
7174   `(gnus-data-mark (gnus-data-find
7175                     ,(or number '(gnus-summary-article-number)))))
7176
7177 (defmacro gnus-summary-article-pos (&optional number)
7178   `(gnus-data-pos (gnus-data-find
7179                    ,(or number '(gnus-summary-article-number)))))
7180
7181 (defalias 'gnus-summary-subject-string 'gnus-summary-article-subject)
7182 (defmacro gnus-summary-article-subject (&optional number)
7183   "Return current subject string or nil if nothing."
7184   `(let ((headers
7185           ,(if number
7186                `(gnus-data-header (assq ,number gnus-newsgroup-data))
7187              '(gnus-data-header (assq (gnus-summary-article-number)
7188                                       gnus-newsgroup-data)))))
7189      (and headers
7190           (vectorp headers)
7191           (mail-header-subject headers))))
7192
7193 (defmacro gnus-summary-article-score (&optional number)
7194   "Return current article score."
7195   `(or (cdr (assq ,(or number '(gnus-summary-article-number))
7196                   gnus-newsgroup-scored))
7197        gnus-summary-default-score 0))
7198
7199 (defun gnus-summary-article-children (&optional number)
7200   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))))
7201          (level (gnus-data-level (car data)))
7202          l children)
7203     (while (and (setq data (cdr data))
7204                 (> (setq l (gnus-data-level (car data))) level))
7205       (and (= (1+ level) l)
7206            (setq children (cons (gnus-data-number (car data))
7207                                 children))))
7208     (nreverse children)))
7209
7210 (defun gnus-summary-article-parent (&optional number)
7211   (let* ((data (gnus-data-find-list (or number (gnus-summary-article-number))
7212                                     (gnus-data-list t)))
7213          (level (gnus-data-level (car data)))
7214          l)
7215     (if (zerop level)
7216         () ; This is a root.
7217       ;; We search until we find an article with a level less than
7218       ;; this one.  That function has to be the parent.
7219       (while (and (setq data (cdr data))
7220                   (not (< (gnus-data-level (car data)) level))))
7221       (and data (gnus-data-number (car data))))))
7222
7223 (defun gnus-unread-mark-p (mark)
7224   "Say whether MARK is the unread mark."
7225   (= mark gnus-unread-mark))
7226
7227 (defun gnus-read-mark-p (mark)
7228   "Say whether MARK is one of the marks that mark as read.
7229 This is all marks except unread, ticked, dormant, and expirable."
7230   (not (or (= mark gnus-unread-mark)
7231            (= mark gnus-ticked-mark)
7232            (= mark gnus-dormant-mark)
7233            (= mark gnus-expirable-mark))))
7234
7235 ;; Various summary mode internalish functions.
7236
7237 (defun gnus-mouse-pick-article (e)
7238   (interactive "e")
7239   (mouse-set-point e)
7240   (gnus-summary-next-page nil t))
7241
7242 (defun gnus-summary-setup-buffer (group)
7243   "Initialize summary buffer."
7244   (let ((buffer (concat "*Summary " group "*")))
7245     (if (get-buffer buffer)
7246         (progn
7247           (set-buffer buffer)
7248           (setq gnus-summary-buffer (current-buffer))
7249           (not gnus-newsgroup-prepared))
7250       ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>
7251       (setq gnus-summary-buffer (set-buffer (get-buffer-create buffer)))
7252       (gnus-add-current-to-buffer-list)
7253       (gnus-summary-mode group)
7254       (when gnus-carpal
7255         (gnus-carpal-setup-buffer 'summary))
7256       (unless gnus-single-article-buffer
7257         (make-local-variable 'gnus-article-buffer)
7258         (make-local-variable 'gnus-original-article-buffer))
7259       (setq gnus-newsgroup-name group)
7260       t)))
7261
7262 (defun gnus-set-global-variables ()
7263   ;; Set the global equivalents of the summary buffer-local variables
7264   ;; to the latest values they had.  These reflect the summary buffer
7265   ;; that was in action when the last article was fetched.
7266   (when (eq major-mode 'gnus-summary-mode)
7267     (setq gnus-summary-buffer (current-buffer))
7268     (let ((name gnus-newsgroup-name)
7269           (marked gnus-newsgroup-marked)
7270           (unread gnus-newsgroup-unreads)
7271           (headers gnus-current-headers)
7272           (data gnus-newsgroup-data)
7273           (summary gnus-summary-buffer)
7274           (article-buffer gnus-article-buffer)
7275           (original gnus-original-article-buffer)
7276           (score-file gnus-current-score-file))
7277       (save-excursion
7278         (set-buffer gnus-group-buffer)
7279         (setq gnus-newsgroup-name name)
7280         (setq gnus-newsgroup-marked marked)
7281         (setq gnus-newsgroup-unreads unread)
7282         (setq gnus-current-headers headers)
7283         (setq gnus-newsgroup-data data)
7284         (setq gnus-summary-buffer summary)
7285         (setq gnus-article-buffer article-buffer)
7286         (setq gnus-original-article-buffer original)
7287         (setq gnus-current-score-file score-file)))))
7288
7289 (defun gnus-summary-last-article-p (&optional article)
7290   "Return whether ARTICLE is the last article in the buffer."
7291   (if (not (setq article (or article (gnus-summary-article-number))))
7292       t ; All non-existant numbers are the last article. :-)
7293     (cdr (gnus-data-find-list article))))
7294
7295 (defun gnus-summary-insert-dummy-line (gnus-tmp-subject gnus-tmp-number)
7296   "Insert a dummy root in the summary buffer."
7297   (beginning-of-line)
7298   (add-text-properties
7299    (point) (progn (eval gnus-summary-dummy-line-format-spec) (point))
7300    (list 'gnus-number gnus-tmp-number 'gnus-intangible gnus-tmp-number)))
7301
7302 (defvar gnus-thread-indent-array nil)
7303 (defvar gnus-thread-indent-array-level gnus-thread-indent-level)
7304 (defun gnus-make-thread-indent-array ()
7305   (let ((n 200))
7306     (if (and gnus-thread-indent-array
7307              (= gnus-thread-indent-level gnus-thread-indent-array-level))
7308         nil
7309       (setq gnus-thread-indent-array (make-vector 201 "")
7310             gnus-thread-indent-array-level gnus-thread-indent-level)
7311       (while (>= n 0)
7312         (aset gnus-thread-indent-array n
7313               (make-string (* n gnus-thread-indent-level) ? ))
7314         (setq n (1- n))))))
7315
7316 (defun gnus-summary-insert-line
7317   (gnus-tmp-header gnus-tmp-level gnus-tmp-current gnus-tmp-unread
7318                    gnus-tmp-replied gnus-tmp-expirable gnus-tmp-subject-or-nil
7319                    &optional gnus-tmp-dummy gnus-tmp-score gnus-tmp-process)
7320   (let* ((gnus-tmp-indentation (aref gnus-thread-indent-array gnus-tmp-level))
7321          (gnus-tmp-lines (mail-header-lines gnus-tmp-header))
7322          (gnus-tmp-score (or gnus-tmp-score gnus-summary-default-score 0))
7323          (gnus-tmp-score-char
7324           (if (or (null gnus-summary-default-score)
7325                   (<= (abs (- gnus-tmp-score gnus-summary-default-score))
7326                       gnus-summary-zcore-fuzz)) ? 
7327             (if (< gnus-tmp-score gnus-summary-default-score)
7328                 gnus-score-below-mark gnus-score-over-mark)))
7329          (gnus-tmp-replied (cond (gnus-tmp-process gnus-process-mark)
7330                                  ((memq gnus-tmp-current gnus-newsgroup-cached)
7331                                   gnus-cached-mark)
7332                                  (gnus-tmp-replied gnus-replied-mark)
7333                                  ((memq gnus-tmp-current gnus-newsgroup-saved)
7334                                   gnus-saved-mark)
7335                                  (t gnus-unread-mark)))
7336          (gnus-tmp-from (mail-header-from gnus-tmp-header))
7337          (gnus-tmp-name
7338           (cond
7339            ((string-match "(.+)" gnus-tmp-from)
7340             (substring gnus-tmp-from
7341                        (1+ (match-beginning 0)) (1- (match-end 0))))
7342            ((string-match "<[^>]+> *$" gnus-tmp-from)
7343             (let ((beg (match-beginning 0)))
7344               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
7345                        (substring gnus-tmp-from (1+ (match-beginning 0))
7346                                   (1- (match-end 0))))
7347                   (substring gnus-tmp-from 0 beg))))
7348            (t gnus-tmp-from)))
7349          (gnus-tmp-subject (mail-header-subject gnus-tmp-header))
7350          (gnus-tmp-number (mail-header-number gnus-tmp-header))
7351          (gnus-tmp-opening-bracket (if gnus-tmp-dummy ?\< ?\[))
7352          (gnus-tmp-closing-bracket (if gnus-tmp-dummy ?\> ?\]))
7353          (buffer-read-only nil))
7354     (when (string= gnus-tmp-name "")
7355       (setq gnus-tmp-name gnus-tmp-from))
7356     (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
7357     (put-text-property
7358      (point)
7359      (progn (eval gnus-summary-line-format-spec) (point))
7360      'gnus-number gnus-tmp-number)
7361     (when (gnus-visual-p 'summary-highlight 'highlight)
7362       (forward-line -1)
7363       (run-hooks 'gnus-summary-update-hook)
7364       (forward-line 1))))
7365
7366 (defun gnus-summary-update-line (&optional dont-update)
7367   ;; Update summary line after change.
7368   (when (and gnus-summary-default-score
7369              (not gnus-summary-inhibit-highlight))
7370     (let* ((gnus-summary-inhibit-highlight t) ; Prevent recursion.
7371            (article (gnus-summary-article-number))
7372            (score (gnus-summary-article-score article)))
7373       (unless dont-update
7374         (if (and gnus-summary-mark-below
7375                  (< (gnus-summary-article-score)
7376                     gnus-summary-mark-below))
7377             ;; This article has a low score, so we mark it as read.
7378             (when (memq article gnus-newsgroup-unreads)
7379               (gnus-summary-mark-article-as-read gnus-low-score-mark))
7380           (when (eq (gnus-summary-article-mark) gnus-low-score-mark)
7381             ;; This article was previously marked as read on account
7382             ;; of a low score, but now it has risen, so we mark it as
7383             ;; unread.
7384             (gnus-summary-mark-article-as-unread gnus-unread-mark)))
7385         (gnus-summary-update-mark
7386          (if (or (null gnus-summary-default-score)
7387                  (<= (abs (- score gnus-summary-default-score))
7388                      gnus-summary-zcore-fuzz)) ? 
7389            (if (< score gnus-summary-default-score)
7390                gnus-score-below-mark gnus-score-over-mark)) 'score))
7391       ;; Do visual highlighting.
7392       (when (gnus-visual-p 'summary-highlight 'highlight)
7393         (run-hooks 'gnus-summary-update-hook)))))
7394
7395 (defvar gnus-tmp-new-adopts nil)
7396
7397 (defun gnus-summary-number-of-articles-in-thread (thread &optional level char)
7398   ;; Sum up all elements (and sub-elements) in a list.
7399   (let* ((number
7400           ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
7401           (cond
7402            ((and (consp thread) (cdr thread))
7403             (apply
7404              '+ 1 (mapcar
7405                    'gnus-summary-number-of-articles-in-thread (cdr thread))))
7406            ((null thread)
7407             1)
7408            ((memq (mail-header-number (car thread)) gnus-newsgroup-limit)
7409             1)
7410            (t 1))))
7411     (when (and level (zerop level) gnus-tmp-new-adopts)
7412       (incf number
7413             (apply '+ (mapcar
7414                        'gnus-summary-number-of-articles-in-thread
7415                        gnus-tmp-new-adopts))))
7416     (if char
7417         (if (> number 1) gnus-not-empty-thread-mark
7418           gnus-empty-thread-mark)
7419       number)))
7420
7421 (defun gnus-summary-set-local-parameters (group)
7422  "Go through the local params of GROUP and set all variable specs in that list."
7423   (let ((params (gnus-info-params (gnus-get-info group)))
7424         elem)
7425     (while params
7426       (setq elem (car params)
7427             params (cdr params))
7428       (and (consp elem)                 ; Has to be a cons.
7429            (consp (cdr elem))           ; The cdr has to be a list.
7430            (symbolp (car elem))         ; Has to be a symbol in there.
7431            (not (memq (car elem) 
7432                       '(quit-config to-address to-list to-group)))
7433            (progn                       ; So we set it.
7434              (make-local-variable (car elem))
7435              (set (car elem) (eval (nth 1 elem))))))))
7436
7437 (defun gnus-summary-read-group
7438   (group &optional show-all no-article kill-buffer no-display)
7439   "Start reading news in newsgroup GROUP.
7440 If SHOW-ALL is non-nil, already read articles are also listed.
7441 If NO-ARTICLE is non-nil, no article is selected initially.
7442 If NO-DISPLAY, don't generate a summary buffer."
7443   (gnus-message 5 "Retrieving newsgroup: %s..." group)
7444   (let* ((new-group (gnus-summary-setup-buffer group))
7445          (quit-config (gnus-group-quit-config group))
7446          (did-select (and new-group (gnus-select-newsgroup group show-all))))
7447     (cond
7448      ;; This summary buffer exists already, so we just select it.
7449      ((not new-group)
7450       (gnus-set-global-variables)
7451       (when kill-buffer
7452         (gnus-kill-or-deaden-summary kill-buffer))
7453       (gnus-configure-windows 'summary 'force)
7454       (gnus-set-mode-line 'summary)
7455       (gnus-summary-position-point)
7456       (message "")
7457       t)
7458      ;; We couldn't select this group.
7459      ((null did-select)
7460       (when (and (eq major-mode 'gnus-summary-mode)
7461                  (not (equal (current-buffer) kill-buffer)))
7462         (kill-buffer (current-buffer))
7463         (if (not quit-config)
7464             (progn
7465               (set-buffer gnus-group-buffer)
7466               (gnus-group-jump-to-group group)
7467               (gnus-group-next-unread-group 1))
7468           (if (not (buffer-name (car quit-config)))
7469               (gnus-configure-windows 'group 'force)
7470             (set-buffer (car quit-config))
7471             (and (eq major-mode 'gnus-summary-mode)
7472                  (gnus-set-global-variables))
7473             (gnus-configure-windows (cdr quit-config)))))
7474       (gnus-message 3 "Can't select group")
7475       nil)
7476      ;; The user did a `C-g' while prompting for number of articles,
7477      ;; so we exit this group.
7478      ((eq did-select 'quit)
7479       (and (eq major-mode 'gnus-summary-mode)
7480            (not (equal (current-buffer) kill-buffer))
7481            (kill-buffer (current-buffer)))
7482       (when kill-buffer
7483         (gnus-kill-or-deaden-summary kill-buffer))
7484       (if (not quit-config)
7485           (progn
7486             (set-buffer gnus-group-buffer)
7487             (gnus-group-jump-to-group group)
7488             (gnus-group-next-unread-group 1)
7489             (gnus-configure-windows 'group 'force))
7490         (if (not (buffer-name (car quit-config)))
7491             (gnus-configure-windows 'group 'force)
7492           (set-buffer (car quit-config))
7493           (and (eq major-mode 'gnus-summary-mode)
7494                (gnus-set-global-variables))
7495           (gnus-configure-windows (cdr quit-config))))
7496       ;; Finally signal the quit.
7497       (signal 'quit nil))
7498      ;; The group was successfully selected.
7499      (t
7500       (gnus-set-global-variables)
7501       ;; Save the active value in effect when the group was entered.
7502       (setq gnus-newsgroup-active
7503             (gnus-copy-sequence
7504              (gnus-active gnus-newsgroup-name)))
7505       ;; You can change the summary buffer in some way with this hook.
7506       (run-hooks 'gnus-select-group-hook)
7507       ;; Set any local variables in the group parameters.
7508       (gnus-summary-set-local-parameters gnus-newsgroup-name)
7509       (gnus-update-format-specifications)
7510       ;; Do score processing.
7511       (when gnus-use-scoring
7512         (gnus-possibly-score-headers))
7513       ;; Check whether to fill in the gaps in the threads.
7514       (when gnus-build-sparse-threads
7515         (gnus-build-sparse-threads))
7516       ;; Find the initial limit.
7517       (if show-all
7518           (let ((gnus-newsgroup-dormant nil))
7519             (gnus-summary-initial-limit show-all))
7520         (gnus-summary-initial-limit show-all))
7521       ;; Generate the summary buffer.
7522       (unless no-display
7523         (gnus-summary-prepare))
7524       (when gnus-use-trees
7525         (gnus-tree-open group)
7526         (setq gnus-summary-highlight-line-function
7527               'gnus-tree-highlight-article))
7528       ;; If the summary buffer is empty, but there are some low-scored
7529       ;; articles or some excluded dormants, we include these in the
7530       ;; buffer.
7531       (when (and (zerop (buffer-size))
7532                  (not no-display))
7533         (cond (gnus-newsgroup-dormant
7534                (gnus-summary-limit-include-dormant))
7535               ((and gnus-newsgroup-scored show-all)
7536                (gnus-summary-limit-include-expunged))))
7537       ;; Function `gnus-apply-kill-file' must be called in this hook.
7538       (run-hooks 'gnus-apply-kill-hook)
7539       (if (and (zerop (buffer-size))
7540                (not no-display))
7541           (progn
7542             ;; This newsgroup is empty.
7543             (gnus-summary-catchup-and-exit nil t) ;Without confirmations.
7544             (gnus-message 6 "No unread news")
7545             (when kill-buffer
7546               (gnus-kill-or-deaden-summary kill-buffer))
7547             ;; Return nil from this function.
7548             nil)
7549         ;; Hide conversation thread subtrees.  We cannot do this in
7550         ;; gnus-summary-prepare-hook since kill processing may not
7551         ;; work with hidden articles.
7552         (and gnus-show-threads
7553              gnus-thread-hide-subtree
7554              (gnus-summary-hide-all-threads))
7555         ;; Show first unread article if requested.
7556         (if (and (not no-article)
7557                  (not no-display)
7558                  gnus-newsgroup-unreads
7559                  gnus-auto-select-first)
7560             (if (eq gnus-auto-select-first 'best)
7561                 (gnus-summary-best-unread-article)
7562               (gnus-summary-first-unread-article))
7563           ;; Don't select any articles, just move point to the first
7564           ;; article in the group.
7565           (goto-char (point-min))
7566           (gnus-summary-position-point)
7567           (gnus-set-mode-line 'summary)
7568           (gnus-configure-windows 'summary 'force))
7569         ;; If we are in async mode, we send some info to the backend.
7570         (when gnus-newsgroup-async
7571           (gnus-request-asynchronous gnus-newsgroup-name gnus-newsgroup-data))
7572         (when kill-buffer
7573           (gnus-kill-or-deaden-summary kill-buffer))
7574         (when (get-buffer-window gnus-group-buffer t)
7575           ;; Gotta use windows, because recenter does wierd stuff if
7576           ;; the current buffer ain't the displayed window.
7577           (let ((owin (selected-window)))
7578             (select-window (get-buffer-window gnus-group-buffer t))
7579             (when (gnus-group-goto-group group)
7580               (recenter))
7581             (select-window owin))))
7582       ;; Mark this buffer as "prepared".
7583       (setq gnus-newsgroup-prepared t)
7584       t))))
7585
7586 (defun gnus-summary-prepare ()
7587   "Generate the summary buffer."
7588   (let ((buffer-read-only nil))
7589     (erase-buffer)
7590     (setq gnus-newsgroup-data nil
7591           gnus-newsgroup-data-reverse nil)
7592     (run-hooks 'gnus-summary-generate-hook)
7593     ;; Generate the buffer, either with threads or without.
7594     (when gnus-newsgroup-headers
7595       (gnus-summary-prepare-threads
7596        (if gnus-show-threads
7597            (gnus-sort-gathered-threads
7598             (funcall gnus-summary-thread-gathering-function
7599                      (gnus-sort-threads
7600                       (gnus-cut-threads (gnus-make-threads)))))
7601          ;; Unthreaded display.
7602          (gnus-sort-articles gnus-newsgroup-headers))))
7603     (setq gnus-newsgroup-data (nreverse gnus-newsgroup-data))
7604     ;; Call hooks for modifying summary buffer.
7605     (goto-char (point-min))
7606     (run-hooks 'gnus-summary-prepare-hook)))
7607
7608 (defun gnus-gather-threads-by-subject (threads)
7609   "Gather threads by looking at Subject headers."
7610   (if (not gnus-summary-make-false-root)
7611       threads
7612     (let ((hashtb (gnus-make-hashtable 1023))
7613           (prev threads)
7614           (result threads)
7615           subject hthread whole-subject)
7616       (while threads
7617         (setq whole-subject (mail-header-subject (caar threads)))
7618         (if (and gnus-summary-gather-exclude-subject
7619                  (string-match gnus-summary-gather-exclude-subject
7620                                whole-subject))
7621             () ; We don't want to do anything with this article.
7622           ;; We simplify the subject before looking it up in the
7623           ;; hash table.
7624           (setq subject
7625                 (cond
7626                  ;; Truncate the subject.
7627                  ((numberp gnus-summary-gather-subject-limit)
7628                   (setq subject (gnus-simplify-subject-re whole-subject))
7629                   (if (> (length subject) gnus-summary-gather-subject-limit)
7630                       (substring subject 0 gnus-summary-gather-subject-limit)
7631                     subject))
7632                  ;; Fuzzily simplify it.
7633                  ((eq 'fuzzy gnus-summary-gather-subject-limit)
7634                   (gnus-simplify-subject-fuzzy whole-subject))
7635                  ;; Just remove the leading "Re:".
7636                  (t
7637                   (gnus-simplify-subject-re whole-subject))))
7638
7639           (if (setq hthread (gnus-gethash subject hashtb))
7640               (progn
7641                 ;; We enter a dummy root into the thread, if we
7642                 ;; haven't done that already.
7643                 (unless (stringp (caar hthread))
7644                   (setcar hthread (list whole-subject (car hthread))))
7645                 ;; We add this new gathered thread to this gathered
7646                 ;; thread.
7647                 (setcdr (car hthread)
7648                         (nconc (cdar hthread) (list (car threads))))
7649                 ;; Remove it from the list of threads.
7650                 (setcdr prev (cdr threads))
7651                 (setq threads prev))
7652             ;; Enter this thread into the hash table.
7653             (gnus-sethash subject threads hashtb)))
7654         (setq prev threads)
7655         (setq threads (cdr threads)))
7656       result)))
7657
7658 (defun gnus-gather-threads-by-references (threads)
7659   "Gather threads by looking at References headers."
7660   (let ((idhashtb (gnus-make-hashtable 1023))
7661         (thhashtb (gnus-make-hashtable 1023))
7662         (prev threads)
7663         (result threads)
7664         ids references id gthread gid entered)
7665     (while threads
7666       (when (setq references (mail-header-references (caar threads)))
7667         (setq id (mail-header-id (caar threads)))
7668         (setq ids (gnus-split-references references))
7669         (setq entered nil)
7670         (while ids
7671           (if (not (setq gid (gnus-gethash (car ids) idhashtb)))
7672               (progn
7673                 (gnus-sethash (car ids) id idhashtb)
7674                 (gnus-sethash id threads thhashtb))
7675             (setq gthread (gnus-gethash gid thhashtb))
7676             (unless entered
7677               ;; We enter a dummy root into the thread, if we
7678               ;; haven't done that already.
7679               (unless (stringp (caar gthread))
7680                 (setcar gthread (list (mail-header-subject (caar gthread))
7681                                       (car gthread))))
7682               ;; We add this new gathered thread to this gathered
7683               ;; thread.
7684               (setcdr (car gthread)
7685                       (nconc (cdar gthread) (list (car threads)))))
7686             ;; Add it into the thread hash table.
7687             (gnus-sethash id gthread thhashtb)
7688             (setq entered t)
7689             ;; Remove it from the list of threads.
7690             (setcdr prev (cdr threads))
7691             (setq threads prev))
7692           (setq ids (cdr ids))))
7693       (setq prev threads)
7694       (setq threads (cdr threads)))
7695     result))
7696
7697 (defun gnus-sort-gathered-threads (threads)
7698   "Sort subtreads inside each gathered thread by article number."
7699   (let ((result threads))
7700     (while threads
7701       (when (stringp (caar threads))
7702         (setcdr (car threads)
7703                 (sort (cdar threads) 'gnus-thread-sort-by-number)))
7704       (setq threads (cdr threads)))
7705     result))
7706
7707 (defun gnus-make-threads ()
7708   "Go through the dependency hashtb and find the roots.  Return all threads."
7709   (let (threads)
7710     (mapatoms
7711      (lambda (refs)
7712        (unless (car (symbol-value refs))
7713          ;; These threads do not refer back to any other articles,
7714          ;; so they're roots.
7715          (setq threads (append (cdr (symbol-value refs)) threads))))
7716      gnus-newsgroup-dependencies)
7717     threads))
7718
7719 (defun gnus-build-sparse-threads ()
7720   (let ((headers gnus-newsgroup-headers)
7721         (deps gnus-newsgroup-dependencies)
7722         header references generation relations 
7723         cthread subject child end pthread relation)
7724     ;; First we create an alist of generations/relations, where 
7725     ;; generations is how much we trust the ralation, and the relation
7726     ;; is parent/child.
7727     (gnus-message 7 "Making sparse threads...")
7728     (save-excursion
7729       (nnheader-set-temp-buffer " *gnus sparse threads*")
7730       (while (setq header (pop headers))
7731         (when (and (setq references (mail-header-references header))
7732                    (not (string= references "")))
7733           (insert references)
7734           (setq child (downcase (mail-header-id header))
7735                 subject (mail-header-subject header))
7736           (setq generation 0)
7737           (while (search-backward ">" nil t)
7738             (setq end (1+ (point)))
7739             (when (search-backward "<" nil t)
7740               (push (list (incf generation) 
7741                           child (setq child (downcase
7742                                              (buffer-substring (point) end)))
7743                           subject)
7744                     relations)))
7745           (push (list (1+ generation) child nil subject) relations)
7746           (erase-buffer)))
7747       (kill-buffer (current-buffer)))
7748     ;; Sort over trustworthiness.
7749     (setq relations (sort relations (lambda (r1 r2) (< (car r1) (car r2)))))
7750     (while (setq relation (pop relations))
7751       (when (if (boundp (setq cthread (intern (cadr relation) deps)))
7752                 (unless (car (symbol-value cthread))
7753                   ;; Make this article the parent of these threads.
7754                   (setcar (symbol-value cthread)
7755                           (vector gnus-reffed-article-number 
7756                                   (cadddr relation) 
7757                                   "" ""
7758                                   (cadr relation) 
7759                                   (or (caddr relation) "") 0 0 "")))
7760               (set cthread (list (vector gnus-reffed-article-number
7761                                          (cadddr relation) 
7762                                          "" "" (cadr relation) 
7763                                          (or (caddr relation) "") 0 0 ""))))
7764         (push gnus-reffed-article-number gnus-newsgroup-limit)
7765         (push gnus-reffed-article-number gnus-newsgroup-sparse)
7766         (push (cons gnus-reffed-article-number gnus-sparse-mark)
7767               gnus-newsgroup-reads)
7768         (decf gnus-reffed-article-number)
7769         ;; Make this new thread the child of its parent.
7770         (if (boundp (setq pthread (intern (or (caddr relation) "none") deps)))
7771             (setcdr (symbol-value pthread)
7772                     (nconc (cdr (symbol-value pthread))
7773                            (list (symbol-value cthread))))
7774           (set pthread (list nil (symbol-value cthread))))))
7775     (gnus-message 7 "Making sparse threads...done")))
7776
7777 (defun gnus-build-old-threads ()
7778   ;; Look at all the articles that refer back to old articles, and
7779   ;; fetch the headers for the articles that aren't there.  This will
7780   ;; build complete threads - if the roots haven't been expired by the
7781   ;; server, that is.
7782   (let (id heads)
7783     (mapatoms
7784      (lambda (refs)
7785        (when (not (car (symbol-value refs)))
7786          (setq heads (cdr (symbol-value refs)))
7787          (while heads
7788            (if (memq (mail-header-number (caar heads))
7789                      gnus-newsgroup-dormant)
7790                (setq heads (cdr heads))
7791              (setq id (symbol-name refs))
7792              (while (and (setq id (gnus-build-get-header id))
7793                          (not (car (gnus-gethash
7794                                     id gnus-newsgroup-dependencies)))))
7795              (setq heads nil)))))
7796      gnus-newsgroup-dependencies)))
7797
7798 (defun gnus-build-get-header (id)
7799   ;; Look through the buffer of NOV lines and find the header to
7800   ;; ID.  Enter this line into the dependencies hash table, and return
7801   ;; the id of the parent article (if any).
7802   (let ((deps gnus-newsgroup-dependencies)
7803         found header)
7804     (prog1
7805         (save-excursion
7806           (set-buffer nntp-server-buffer)
7807           (goto-char (point-min))
7808           (while (and (not found) (search-forward id nil t))
7809             (beginning-of-line)
7810             (setq found (looking-at
7811                          (format "^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t%s"
7812                                  (regexp-quote id))))
7813             (or found (beginning-of-line 2)))
7814           (when found
7815             (let (ref)
7816               (beginning-of-line)
7817               (and
7818                (setq header (gnus-nov-parse-line
7819                              (read (current-buffer)) deps))
7820                (gnus-parent-id (mail-header-references header))))))
7821       (when header
7822         (let ((number (mail-header-number header)))
7823           (push number gnus-newsgroup-limit)
7824           (push header gnus-newsgroup-headers)
7825           (if (memq number gnus-newsgroup-unselected)
7826               (progn
7827                 (push number gnus-newsgroup-unreads)
7828                 (setq gnus-newsgroup-unselected
7829                       (delq number gnus-newsgroup-unselected)))
7830             (push number gnus-newsgroup-ancient)))))))
7831
7832 (defun gnus-summary-update-article (article &optional header)
7833   "Update ARTICLE in the summary buffer."
7834   (let ((id (mail-header-id (gnus-summary-article-header article)))
7835         (data (gnus-data-find article)))
7836     (setcar (gnus-id-to-thread id) nil)
7837     (gnus-summary-insert-subject id)
7838     ;; Set the (possibly) new article number in the data structure.
7839     (gnus-data-set-number data (gnus-id-to-article id))))
7840
7841 (defun gnus-rebuild-thread (id)
7842   "Rebuild the thread containing ID."
7843   (let ((dep gnus-newsgroup-dependencies)
7844         (buffer-read-only nil)
7845         current headers refs thread art data)
7846     (if (not gnus-show-threads)
7847         (setq thread (list (car (gnus-id-to-thread id))))
7848       ;; Get the thread this article is part of.
7849       (setq thread (gnus-remove-thread id)))
7850     (setq current (save-excursion
7851                     (and (zerop (forward-line -1))
7852                          (gnus-summary-article-number))))
7853     ;; If this is a gathered thread, we have to go some re-gathering.
7854     (when (stringp (car thread))
7855       (let ((subject (car thread))
7856             roots thr)
7857         (setq thread (cdr thread))
7858         (while thread
7859           (unless (memq (setq thr (gnus-id-to-thread
7860                                       (gnus-root-id
7861                                        (mail-header-id (caar thread)))))
7862                         roots)
7863             (push thr roots))
7864           (setq thread (cdr thread)))
7865         ;; We now have all (unique) roots.
7866         (if (= (length roots) 1)
7867             ;; All the loose roots are now one solid root.
7868             (setq thread (car roots))
7869           (setq thread (cons subject (gnus-sort-threads roots))))))
7870     (let ((beg (point))
7871           threads)
7872       ;; We then insert this thread into the summary buffer.
7873       (let (gnus-newsgroup-data gnus-newsgroup-threads)
7874         (gnus-summary-prepare-threads (list thread))
7875         (setq data (nreverse gnus-newsgroup-data))
7876         (setq threads gnus-newsgroup-threads))
7877       ;; We splice the new data into the data structure.
7878       (gnus-data-enter-list current data)
7879       (gnus-data-compute-positions)
7880       (setq gnus-newsgroup-threads (nconc threads gnus-newsgroup-threads)))))
7881
7882 (defun gnus-id-to-thread (id)
7883   "Return the (sub-)thread where ID appears."
7884   (gnus-gethash (downcase id) gnus-newsgroup-dependencies))
7885
7886 (defun gnus-id-to-article (id)
7887   "Return the article number of ID."
7888   (let ((thread (gnus-id-to-thread id)))
7889     (when thread
7890       (mail-header-number (car thread)))))
7891
7892 (defun gnus-id-to-header (id)
7893   "Return the article headers of ID."
7894   (car (gnus-id-to-thread id)))
7895
7896 (defun gnus-article-displayed-root-p (article)
7897   "Say whether ARTICLE is a root(ish) article."
7898   (let ((level (gnus-summary-thread-level article))
7899         particle)
7900     (cond 
7901      ((null level) nil)
7902      ((zerop level) t)
7903      ((and (= 1 level)
7904            (null (setq particle (gnus-id-to-article
7905                                  (gnus-parent-id 
7906                                   (mail-header-references 
7907                                    (gnus-summary-article-header article))))))
7908            (null (gnus-summary-thread-level particle)))))))
7909
7910 (defun gnus-root-id (id)
7911   "Return the id of the root of the thread where ID appears."
7912   (let (last-id prev)
7913     (while (and id (setq prev (car (gnus-gethash
7914                                     (downcase id)
7915                                     gnus-newsgroup-dependencies))))
7916       (setq last-id id
7917             id (gnus-parent-id (mail-header-references prev))))
7918     last-id))
7919
7920 (defun gnus-remove-thread (id &optional dont-remove)
7921   "Remove the thread that has ID in it."
7922   (let ((dep gnus-newsgroup-dependencies)
7923         headers thread prev last-id)
7924     ;; First go up in this thread until we find the root.
7925     (setq last-id (gnus-root-id id))
7926     (setq headers (list (car (gnus-id-to-thread last-id))
7927                         (caadr (gnus-id-to-thread last-id))))
7928     ;; We have now found the real root of this thread.  It might have
7929     ;; been gathered into some loose thread, so we have to search
7930     ;; through the threads to find the thread we wanted.
7931     (let ((threads gnus-newsgroup-threads)
7932           sub)
7933       (while threads
7934         (setq sub (car threads))
7935         (if (stringp (car sub))
7936             ;; This is a gathered threads, so we look at the roots
7937             ;; below it to find whether this article in in this
7938             ;; gathered root.
7939             (progn
7940               (setq sub (cdr sub))
7941               (while sub
7942                 (when (member (caar sub) headers)
7943                   (setq thread (car threads)
7944                         threads nil
7945                         sub nil))
7946                 (setq sub (cdr sub))))
7947           ;; It's an ordinary thread, so we check it.
7948           (when (eq (car sub) (car headers))
7949             (setq thread sub
7950                   threads nil)))
7951         (setq threads (cdr threads)))
7952       ;; If this article is in no thread, then it's a root.
7953       (if thread
7954           (unless dont-remove
7955             (setq gnus-newsgroup-threads (delq thread gnus-newsgroup-threads)))
7956         (setq thread (gnus-gethash (downcase last-id) dep)))
7957       (when thread
7958         (prog1
7959             thread ; We return this thread.
7960           (unless dont-remove
7961             (if (stringp (car thread))
7962                 (progn
7963                   ;; If we use dummy roots, then we have to remove the
7964                   ;; dummy root as well.
7965                   (when (eq gnus-summary-make-false-root 'dummy)
7966                     ;; Uhm.
7967                     )
7968                   (setq thread (cdr thread))
7969                   (while thread
7970                     (gnus-remove-thread-1 (car thread))
7971                     (setq thread (cdr thread))))
7972               (gnus-remove-thread-1 thread))))))))
7973
7974 (defun gnus-remove-thread-1 (thread)
7975   "Remove the thread THREAD recursively."
7976   (let ((number (mail-header-number (car thread)))
7977         pos)
7978     (when (setq pos (text-property-any
7979                      (point-min) (point-max) 'gnus-number number))
7980       (goto-char pos)
7981       (gnus-delete-line)
7982       (gnus-data-remove number))
7983     (setq thread (cdr thread))
7984     (while thread
7985       (gnus-remove-thread-1 (car thread))
7986       (setq thread (cdr thread)))))
7987
7988 (defun gnus-sort-threads (threads)
7989   "Sort THREADS."
7990   (if (not gnus-thread-sort-functions)
7991       threads
7992     (let ((func (if (= 1 (length gnus-thread-sort-functions))
7993                     (car gnus-thread-sort-functions)
7994                   `(lambda (t1 t2)
7995                      ,(gnus-make-sort-function 
7996                        (reverse gnus-thread-sort-functions))))))
7997       (gnus-message 7 "Sorting threads...")
7998       (prog1
7999           (sort threads func)
8000         (gnus-message 7 "Sorting threads...done")))))
8001
8002 (defun gnus-sort-articles (articles)
8003   "Sort ARTICLES."
8004   (when gnus-article-sort-functions
8005     (let ((func (if (= 1 (length gnus-article-sort-functions))
8006                     (car gnus-article-sort-functions)
8007                   `(lambda (t1 t2)
8008                      ,(gnus-make-sort-function 
8009                        (reverse gnus-article-sort-functions))))))
8010       (gnus-message 7 "Sorting articles...")
8011       (prog1
8012           (setq gnus-newsgroup-headers (sort articles func))
8013         (gnus-message 7 "Sorting articles...done")))))
8014
8015 (defun gnus-make-sort-function (funs)
8016   "Return a composite sort condition based on the functions in FUNC."
8017   (if (cdr funs)
8018       `(or (,(car funs) t1 t2)
8019            (and (not (,(car funs) t2 t1))
8020                 ,(gnus-make-sort-function (cdr funs))))
8021     `(,(car funs) t1 t2)))
8022                  
8023 ;; Written by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
8024 (defmacro gnus-thread-header (thread)
8025   ;; Return header of first article in THREAD.
8026   ;; Note that THREAD must never, ever be anything else than a variable -
8027   ;; using some other form will lead to serious barfage.
8028   (or (symbolp thread) (signal 'wrong-type-argument '(symbolp thread)))
8029   ;; (8% speedup to gnus-summary-prepare, just for fun :-)
8030   (list 'byte-code "\10\211:\203\17\0\211@;\203\16\0A@@\207" ;
8031         (vector thread) 2))
8032
8033 (defsubst gnus-article-sort-by-number (h1 h2)
8034   "Sort articles by article number."
8035   (< (mail-header-number h1)
8036      (mail-header-number h2)))
8037
8038 (defun gnus-thread-sort-by-number (h1 h2)
8039   "Sort threads by root article number."
8040   (gnus-article-sort-by-number
8041    (gnus-thread-header h1) (gnus-thread-header h2)))
8042
8043 (defsubst gnus-article-sort-by-author (h1 h2)
8044   "Sort articles by root author."
8045   (string-lessp
8046    (let ((extract (funcall
8047                    gnus-extract-address-components
8048                    (mail-header-from h1))))
8049      (or (car extract) (cdr extract)))
8050    (let ((extract (funcall
8051                    gnus-extract-address-components
8052                    (mail-header-from h2))))
8053      (or (car extract) (cdr extract)))))
8054
8055 (defun gnus-thread-sort-by-author (h1 h2)
8056   "Sort threads by root author."
8057   (gnus-article-sort-by-author
8058    (gnus-thread-header h1)  (gnus-thread-header h2)))
8059
8060 (defsubst gnus-article-sort-by-subject (h1 h2)
8061   "Sort articles by root subject."
8062   (string-lessp
8063    (downcase (gnus-simplify-subject-re (mail-header-subject h1)))
8064    (downcase (gnus-simplify-subject-re (mail-header-subject h2)))))
8065
8066 (defun gnus-thread-sort-by-subject (h1 h2)
8067   "Sort threads by root subject."
8068   (gnus-article-sort-by-subject
8069    (gnus-thread-header h1) (gnus-thread-header h2)))
8070
8071 (defsubst gnus-article-sort-by-date (h1 h2)
8072   "Sort articles by root article date."
8073   (string-lessp
8074    (gnus-sortable-date (mail-header-date h1))
8075    (gnus-sortable-date (mail-header-date h2))))
8076
8077 (defun gnus-thread-sort-by-date (h1 h2)
8078   "Sort threads by root article date."
8079   (gnus-article-sort-by-date
8080    (gnus-thread-header h1) (gnus-thread-header h2)))
8081
8082 (defsubst gnus-article-sort-by-score (h1 h2)
8083   "Sort articles by root article score.
8084 Unscored articles will be counted as having a score of zero."
8085   (> (or (cdr (assq (mail-header-number h1)
8086                     gnus-newsgroup-scored))
8087          gnus-summary-default-score 0)
8088      (or (cdr (assq (mail-header-number h2)
8089                     gnus-newsgroup-scored))
8090          gnus-summary-default-score 0)))
8091
8092 (defun gnus-thread-sort-by-score (h1 h2)
8093   "Sort threads by root article score."
8094   (gnus-article-sort-by-score
8095    (gnus-thread-header h1) (gnus-thread-header h2)))
8096
8097 (defun gnus-thread-sort-by-total-score (h1 h2)
8098   "Sort threads by the sum of all scores in the thread.
8099 Unscored articles will be counted as having a score of zero."
8100   (> (gnus-thread-total-score h1) (gnus-thread-total-score h2)))
8101
8102 (defun gnus-thread-total-score (thread)
8103   ;;  This function find the total score of THREAD.
8104   (if (consp thread)
8105       (if (stringp (car thread))
8106           (apply gnus-thread-score-function 0
8107                  (mapcar 'gnus-thread-total-score-1 (cdr thread)))
8108         (gnus-thread-total-score-1 thread))
8109     (gnus-thread-total-score-1 (list thread))))
8110
8111 (defun gnus-thread-total-score-1 (root)
8112   ;; This function find the total score of the thread below ROOT.
8113   (setq root (car root))
8114   (apply gnus-thread-score-function
8115          (or (cdr (assq (mail-header-number root) gnus-newsgroup-scored))
8116              gnus-summary-default-score 0)
8117          (mapcar 'gnus-thread-total-score
8118                  (cdr (gnus-gethash (downcase (mail-header-id root))
8119                                     gnus-newsgroup-dependencies)))))
8120
8121 ;; Added by Per Abrahamsen <amanda@iesd.auc.dk>.
8122 (defvar gnus-tmp-prev-subject nil)
8123 (defvar gnus-tmp-false-parent nil)
8124 (defvar gnus-tmp-root-expunged nil)
8125 (defvar gnus-tmp-dummy-line nil)
8126
8127 (defun gnus-summary-prepare-threads (threads)
8128   "Prepare summary buffer from THREADS and indentation LEVEL.
8129 THREADS is either a list of `(PARENT [(CHILD1 [(GRANDCHILD ...]...) ...])'
8130 or a straight list of headers."
8131   (gnus-message 7 "Generating summary...")
8132
8133   (setq gnus-newsgroup-threads threads)
8134   (beginning-of-line)
8135
8136   (let ((gnus-tmp-level 0)
8137         (default-score (or gnus-summary-default-score 0))
8138         (gnus-visual-p (gnus-visual-p 'summary-highlight 'highlight))
8139         thread number subject stack state gnus-tmp-gathered beg-match
8140         new-roots gnus-tmp-new-adopts thread-end
8141         gnus-tmp-header gnus-tmp-unread
8142         gnus-tmp-replied gnus-tmp-subject-or-nil
8143         gnus-tmp-dummy gnus-tmp-indentation gnus-tmp-lines gnus-tmp-score
8144         gnus-tmp-score-char gnus-tmp-from gnus-tmp-name
8145         gnus-tmp-number gnus-tmp-opening-bracket gnus-tmp-closing-bracket)
8146
8147     (setq gnus-tmp-prev-subject nil)
8148
8149     (if (vectorp (car threads))
8150         ;; If this is a straight (sic) list of headers, then a
8151         ;; threaded summary display isn't required, so we just create
8152         ;; an unthreaded one.
8153         (gnus-summary-prepare-unthreaded threads)
8154
8155       ;; Do the threaded display.
8156
8157       (while (or threads stack gnus-tmp-new-adopts new-roots)
8158
8159         (if (and (= gnus-tmp-level 0)
8160                  (not (setq gnus-tmp-dummy-line nil))
8161                  (or (not stack)
8162                      (= (caar stack) 0))
8163                  (not gnus-tmp-false-parent)
8164                  (or gnus-tmp-new-adopts new-roots))
8165             (if gnus-tmp-new-adopts
8166                 (setq gnus-tmp-level (if gnus-tmp-root-expunged 0 1)
8167                       thread (list (car gnus-tmp-new-adopts))
8168                       gnus-tmp-header (caar thread)
8169                       gnus-tmp-new-adopts (cdr gnus-tmp-new-adopts))
8170               (if new-roots
8171                   (setq thread (list (car new-roots))
8172                         gnus-tmp-header (caar thread)
8173                         new-roots (cdr new-roots))))
8174
8175           (if threads
8176               ;; If there are some threads, we do them before the
8177               ;; threads on the stack.
8178               (setq thread threads
8179                     gnus-tmp-header (caar thread))
8180             ;; There were no current threads, so we pop something off
8181             ;; the stack.
8182             (setq state (car stack)
8183                   gnus-tmp-level (car state)
8184                   thread (cdr state)
8185                   stack (cdr stack)
8186                   gnus-tmp-header (caar thread))))
8187
8188         (setq gnus-tmp-false-parent nil)
8189         (setq gnus-tmp-root-expunged nil)
8190         (setq thread-end nil)
8191
8192         (if (stringp gnus-tmp-header)
8193             ;; The header is a dummy root.
8194             (cond
8195              ((eq gnus-summary-make-false-root 'adopt)
8196               ;; We let the first article adopt the rest.
8197               (setq gnus-tmp-new-adopts (nconc gnus-tmp-new-adopts
8198                                                (cddar thread)))
8199               (setq gnus-tmp-gathered
8200                     (nconc (mapcar
8201                             (lambda (h) (mail-header-number (car h)))
8202                             (cddar thread))
8203                            gnus-tmp-gathered))
8204               (setq thread (cons (list (caar thread)
8205                                        (cadar thread))
8206                                  (cdr thread)))
8207               (setq gnus-tmp-level -1
8208                     gnus-tmp-false-parent t))
8209              ((eq gnus-summary-make-false-root 'empty)
8210               ;; We print adopted articles with empty subject fields.
8211               (setq gnus-tmp-gathered
8212                     (nconc (mapcar
8213                             (lambda (h) (mail-header-number (car h)))
8214                             (cddar thread))
8215                            gnus-tmp-gathered))
8216               (setq gnus-tmp-level -1))
8217              ((eq gnus-summary-make-false-root 'dummy)
8218               ;; We remember that we probably want to output a dummy
8219               ;; root.
8220               (setq gnus-tmp-dummy-line gnus-tmp-header)
8221               (setq gnus-tmp-prev-subject gnus-tmp-header))
8222              (t
8223               ;; We do not make a root for the gathered
8224               ;; sub-threads at all.
8225               (setq gnus-tmp-level -1)))
8226
8227           (setq number (mail-header-number gnus-tmp-header)
8228                 subject (mail-header-subject gnus-tmp-header))
8229
8230           (cond
8231            ;; If the thread has changed subject, we might want to make
8232            ;; this subthread into a root.
8233            ((and (null gnus-thread-ignore-subject)
8234                  (not (zerop gnus-tmp-level))
8235                  gnus-tmp-prev-subject
8236                  (not (inline
8237                         (gnus-subject-equal gnus-tmp-prev-subject subject))))
8238             (setq new-roots (nconc new-roots (list (car thread)))
8239                   thread-end t
8240                   gnus-tmp-header nil))
8241            ;; If the article lies outside the current limit,
8242            ;; then we do not display it.
8243            ((and (not (memq number gnus-newsgroup-limit))
8244                  (not gnus-tmp-dummy-line))
8245             (setq gnus-tmp-gathered
8246                   (nconc (mapcar
8247                           (lambda (h) (mail-header-number (car h)))
8248                           (cdar thread))
8249                          gnus-tmp-gathered))
8250             (setq gnus-tmp-new-adopts (if (cdar thread)
8251                                           (append gnus-tmp-new-adopts
8252                                                   (cdar thread))
8253                                         gnus-tmp-new-adopts)
8254                   thread-end t
8255                   gnus-tmp-header nil)
8256             (when (zerop gnus-tmp-level)
8257               (setq gnus-tmp-root-expunged t)))
8258            ;; Perhaps this article is to be marked as read?
8259            ((and gnus-summary-mark-below
8260                  (< (or (cdr (assq number gnus-newsgroup-scored))
8261                         default-score)
8262                     gnus-summary-mark-below)
8263                  ;; Don't touch sparse articles.
8264                  (not (memq number gnus-newsgroup-sparse)))
8265             (setq gnus-newsgroup-unreads
8266                   (delq number gnus-newsgroup-unreads))
8267             (if gnus-newsgroup-auto-expire
8268                 (push number gnus-newsgroup-expirable)
8269               (push (cons number gnus-low-score-mark)
8270                     gnus-newsgroup-reads))))
8271
8272           (when gnus-tmp-header
8273             ;; We may have an old dummy line to output before this
8274             ;; article.
8275             (when gnus-tmp-dummy-line
8276               (gnus-summary-insert-dummy-line
8277                gnus-tmp-dummy-line (mail-header-number gnus-tmp-header))
8278               (setq gnus-tmp-dummy-line nil))
8279
8280             ;; Compute the mark.
8281             (setq
8282              gnus-tmp-unread
8283              (cond
8284               ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8285               ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8286               ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8287               ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8288               (t (or (cdr (assq number gnus-newsgroup-reads))
8289                      gnus-ancient-mark))))
8290
8291             (push (gnus-data-make number gnus-tmp-unread (1+ (point))
8292                                   gnus-tmp-header gnus-tmp-level)
8293                   gnus-newsgroup-data)
8294
8295             ;; Actually insert the line.
8296             (setq
8297              gnus-tmp-subject-or-nil
8298              (cond
8299               ((and gnus-thread-ignore-subject
8300                     gnus-tmp-prev-subject
8301                     (not (inline (gnus-subject-equal
8302                                   gnus-tmp-prev-subject subject))))
8303                subject)
8304               ((zerop gnus-tmp-level)
8305                (if (and (eq gnus-summary-make-false-root 'empty)
8306                         (memq number gnus-tmp-gathered)
8307                         gnus-tmp-prev-subject
8308                         (inline (gnus-subject-equal
8309                                  gnus-tmp-prev-subject subject)))
8310                    gnus-summary-same-subject
8311                  subject))
8312               (t gnus-summary-same-subject)))
8313             (if (and (eq gnus-summary-make-false-root 'adopt)
8314                      (= gnus-tmp-level 1)
8315                      (memq number gnus-tmp-gathered))
8316                 (setq gnus-tmp-opening-bracket ?\<
8317                       gnus-tmp-closing-bracket ?\>)
8318               (setq gnus-tmp-opening-bracket ?\[
8319                     gnus-tmp-closing-bracket ?\]))
8320             (setq
8321              gnus-tmp-indentation
8322              (aref gnus-thread-indent-array gnus-tmp-level)
8323              gnus-tmp-lines (mail-header-lines gnus-tmp-header)
8324              gnus-tmp-score (or (cdr (assq number gnus-newsgroup-scored))
8325                                 gnus-summary-default-score 0)
8326              gnus-tmp-score-char
8327              (if (or (null gnus-summary-default-score)
8328                      (<= (abs (- gnus-tmp-score gnus-summary-default-score))
8329                          gnus-summary-zcore-fuzz)) ? 
8330                (if (< gnus-tmp-score gnus-summary-default-score)
8331                    gnus-score-below-mark gnus-score-over-mark))
8332              gnus-tmp-replied
8333              (cond ((memq number gnus-newsgroup-processable)
8334                     gnus-process-mark)
8335                    ((memq number gnus-newsgroup-cached)
8336                     gnus-cached-mark)
8337                    ((memq number gnus-newsgroup-replied)
8338                     gnus-replied-mark)
8339                    (t gnus-unread-mark))
8340              gnus-tmp-from (mail-header-from gnus-tmp-header)
8341              gnus-tmp-name
8342              (cond
8343               ((string-match "(.+)" gnus-tmp-from)
8344                (substring gnus-tmp-from
8345                           (1+ (match-beginning 0)) (1- (match-end 0))))
8346               ((string-match "<[^>]+> *$" gnus-tmp-from)
8347                (setq beg-match (match-beginning 0))
8348                (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
8349                         (substring gnus-tmp-from (1+ (match-beginning 0))
8350                                    (1- (match-end 0))))
8351                    (substring gnus-tmp-from 0 beg-match)))
8352               (t gnus-tmp-from)))
8353             (when (string= gnus-tmp-name "")
8354               (setq gnus-tmp-name gnus-tmp-from))
8355             (or (numberp gnus-tmp-lines) (setq gnus-tmp-lines 0))
8356             (put-text-property
8357              (point)
8358              (progn (eval gnus-summary-line-format-spec) (point))
8359              'gnus-number number)
8360             (when gnus-visual-p
8361               (forward-line -1)
8362               (run-hooks 'gnus-summary-update-hook)
8363               (forward-line 1))
8364
8365             (setq gnus-tmp-prev-subject subject)))
8366
8367         (when (nth 1 thread)
8368           (push (cons (max 0 gnus-tmp-level) (nthcdr 1 thread)) stack))
8369         (incf gnus-tmp-level)
8370         (setq threads (if thread-end nil (cdar thread)))
8371         (unless threads
8372           (setq gnus-tmp-level 0)))))
8373   (gnus-message 7 "Generating summary...done"))
8374
8375 (defun gnus-summary-prepare-unthreaded (headers)
8376   "Generate an unthreaded summary buffer based on HEADERS."
8377   (let (header number mark)
8378
8379     (while headers
8380       (setq header (car headers)
8381             headers (cdr headers)
8382             number (mail-header-number header))
8383
8384       ;; We may have to root out some bad articles...
8385       (when (memq number gnus-newsgroup-limit)
8386         (when (and gnus-summary-mark-below
8387                    (< (or (cdr (assq number gnus-newsgroup-scored))
8388                           gnus-summary-default-score 0)
8389                       gnus-summary-mark-below))
8390           (setq gnus-newsgroup-unreads
8391                 (delq number gnus-newsgroup-unreads))
8392           (if gnus-newsgroup-auto-expire
8393               (push number gnus-newsgroup-expirable)
8394             (push (cons number gnus-low-score-mark)
8395                   gnus-newsgroup-reads)))
8396
8397         (setq mark
8398               (cond
8399                ((memq number gnus-newsgroup-marked) gnus-ticked-mark)
8400                ((memq number gnus-newsgroup-dormant) gnus-dormant-mark)
8401                ((memq number gnus-newsgroup-unreads) gnus-unread-mark)
8402                ((memq number gnus-newsgroup-expirable) gnus-expirable-mark)
8403                (t (or (cdr (assq number gnus-newsgroup-reads))
8404                       gnus-ancient-mark))))
8405         (setq gnus-newsgroup-data
8406               (cons (gnus-data-make number mark (1+ (point)) header 0)
8407                     gnus-newsgroup-data))
8408         (gnus-summary-insert-line
8409          header 0 nil mark (memq number gnus-newsgroup-replied)
8410          (memq number gnus-newsgroup-expirable)
8411          (mail-header-subject header) nil
8412          (cdr (assq number gnus-newsgroup-scored))
8413          (memq number gnus-newsgroup-processable))))))
8414
8415 (defun gnus-select-newsgroup (group &optional read-all)
8416   "Select newsgroup GROUP.
8417 If READ-ALL is non-nil, all articles in the group are selected."
8418   (let* ((entry (gnus-gethash group gnus-newsrc-hashtb))
8419          (info (nth 2 entry))
8420          articles fetched-articles cached)
8421
8422     (or (gnus-check-server
8423          (setq gnus-current-select-method (gnus-find-method-for-group group)))
8424         (error "Couldn't open server"))
8425
8426     (or (and entry (not (eq (car entry) t))) ; Either it's active...
8427         (gnus-activate-group group) ; Or we can activate it...
8428         (progn ; Or we bug out.
8429           (kill-buffer (current-buffer))
8430           (error "Couldn't request group %s: %s"
8431                  group (gnus-status-message group))))
8432
8433     (setq gnus-newsgroup-name group)
8434     (setq gnus-newsgroup-unselected nil)
8435     (setq gnus-newsgroup-unreads (gnus-list-of-unread-articles group))
8436
8437     (and gnus-asynchronous
8438          (gnus-check-backend-function
8439           'request-asynchronous gnus-newsgroup-name)
8440          (setq gnus-newsgroup-async
8441                (gnus-request-asynchronous gnus-newsgroup-name)))
8442
8443     ;; Adjust and set lists of article marks.
8444     (when info
8445       (gnus-adjust-marked-articles info))
8446
8447     ;; Kludge to avoid having cached articles nixed out in virtual groups.
8448     (when (gnus-virtual-group-p group)
8449       (setq cached gnus-newsgroup-cached))
8450
8451     (setq gnus-newsgroup-unreads
8452           (gnus-set-difference
8453            (gnus-set-difference gnus-newsgroup-unreads gnus-newsgroup-marked)
8454            gnus-newsgroup-dormant))
8455
8456     (setq gnus-newsgroup-processable nil)
8457
8458     (setq articles (gnus-articles-to-read group read-all))
8459
8460     (cond
8461      ((null articles)
8462       ;;(gnus-message 3 "Couldn't select newsgroup -- no articles to display")
8463       'quit)
8464      ((eq articles 0) nil)
8465      (t
8466       ;; Init the dependencies hash table.
8467       (setq gnus-newsgroup-dependencies
8468             (gnus-make-hashtable (length articles)))
8469       ;; Retrieve the headers and read them in.
8470       (gnus-message 5 "Fetching headers...")
8471       (setq gnus-newsgroup-headers
8472             (if (eq 'nov
8473                     (setq gnus-headers-retrieved-by
8474                           (gnus-retrieve-headers
8475                            articles gnus-newsgroup-name
8476                            ;; We might want to fetch old headers, but
8477                            ;; not if there is only 1 article.
8478                            (and gnus-fetch-old-headers
8479                                 (or (and
8480                                      (not (eq gnus-fetch-old-headers 'some))
8481                                      (not (numberp gnus-fetch-old-headers)))
8482                                     (> (length articles) 1))))))
8483                 (gnus-get-newsgroup-headers-xover articles)
8484               (gnus-get-newsgroup-headers)))
8485       (gnus-message 5 "Fetching headers...done")
8486
8487       ;; Kludge to avoid having cached articles nixed out in virtual groups.
8488       (when cached
8489         (setq gnus-newsgroup-cached cached))
8490
8491       ;; Set the initial limit.
8492       (setq gnus-newsgroup-limit (copy-sequence articles))
8493       ;; Remove canceled articles from the list of unread articles.
8494       (setq gnus-newsgroup-unreads
8495             (gnus-set-sorted-intersection
8496              gnus-newsgroup-unreads
8497              (setq fetched-articles
8498                    (mapcar (lambda (headers) (mail-header-number headers))
8499                            gnus-newsgroup-headers))))
8500       ;; Removed marked articles that do not exist.
8501       (gnus-update-missing-marks
8502        (gnus-sorted-complement fetched-articles articles))
8503       ;; We might want to build some more threads first.
8504       (and gnus-fetch-old-headers
8505            (eq gnus-headers-retrieved-by 'nov)
8506            (gnus-build-old-threads))
8507       ;; Check whether auto-expire is to be done in this group.
8508       (setq gnus-newsgroup-auto-expire
8509             (gnus-group-auto-expirable-p group))
8510       ;; First and last article in this newsgroup.
8511       (and gnus-newsgroup-headers
8512            (setq gnus-newsgroup-begin
8513                  (mail-header-number (car gnus-newsgroup-headers)))
8514            (setq gnus-newsgroup-end
8515                  (mail-header-number
8516                   (gnus-last-element gnus-newsgroup-headers))))
8517       (setq gnus-reffed-article-number -1)
8518       ;; GROUP is successfully selected.
8519       (or gnus-newsgroup-headers t)))))
8520
8521 (defun gnus-articles-to-read (group read-all)
8522   ;; Find out what articles the user wants to read.
8523   (let* ((articles
8524           ;; Select all articles if `read-all' is non-nil, or if there
8525           ;; are no unread articles.
8526           (if (or read-all
8527                   (and (zerop (length gnus-newsgroup-marked))
8528                        (zerop (length gnus-newsgroup-unreads))))
8529               (gnus-uncompress-range (gnus-active group))
8530             (sort (append gnus-newsgroup-dormant gnus-newsgroup-marked
8531                           (copy-sequence gnus-newsgroup-unreads))
8532                   '<)))
8533          (scored-list (gnus-killed-articles gnus-newsgroup-killed articles))
8534          (scored (length scored-list))
8535          (number (length articles))
8536          (marked (+ (length gnus-newsgroup-marked)
8537                     (length gnus-newsgroup-dormant)))
8538          (select
8539           (cond
8540            ((numberp read-all)
8541             read-all)
8542            (t
8543             (condition-case ()
8544                 (cond
8545                  ((and (or (<= scored marked) (= scored number))
8546                        (numberp gnus-large-newsgroup)
8547                        (> number gnus-large-newsgroup))
8548                   (let ((input
8549                          (read-string
8550                           (format
8551                            "How many articles from %s (default %d): "
8552                            gnus-newsgroup-name number))))
8553                     (if (string-match "^[ \t]*$" input) number input)))
8554                  ((and (> scored marked) (< scored number))
8555                   (let ((input
8556                          (read-string
8557                           (format "%s %s (%d scored, %d total): "
8558                                   "How many articles from"
8559                                   group scored number))))
8560                     (if (string-match "^[ \t]*$" input)
8561                         number input)))
8562                  (t number))
8563               (quit nil))))))
8564     (setq select (if (stringp select) (string-to-number select) select))
8565     (if (or (null select) (zerop select))
8566         select
8567       (if (and (not (zerop scored)) (<= (abs select) scored))
8568           (progn
8569             (setq articles (sort scored-list '<))
8570             (setq number (length articles)))
8571         (setq articles (copy-sequence articles)))
8572
8573       (if (< (abs select) number)
8574           (if (< select 0)
8575               ;; Select the N oldest articles.
8576               (setcdr (nthcdr (1- (abs select)) articles) nil)
8577             ;; Select the N most recent articles.
8578             (setq articles (nthcdr (- number select) articles))))
8579       (setq gnus-newsgroup-unselected
8580             (gnus-sorted-intersection
8581              gnus-newsgroup-unreads
8582              (gnus-sorted-complement gnus-newsgroup-unreads articles)))
8583       articles)))
8584
8585 (defun gnus-killed-articles (killed articles)
8586   (let (out)
8587     (while articles
8588       (if (inline (gnus-member-of-range (car articles) killed))
8589           (setq out (cons (car articles) out)))
8590       (setq articles (cdr articles)))
8591     out))
8592
8593 (defun gnus-uncompress-marks (marks)
8594   "Uncompress the mark ranges in MARKS."
8595   (let ((uncompressed '(score bookmark))
8596         out)
8597     (while marks
8598       (if (memq (caar marks) uncompressed)
8599           (push (car marks) out)
8600         (push (cons (caar marks) (gnus-uncompress-range (cdar marks))) out))
8601       (setq marks (cdr marks)))
8602     out))
8603
8604 (defun gnus-adjust-marked-articles (info)
8605   "Set all article lists and remove all marks that are no longer legal."
8606   (let* ((marked-lists (gnus-info-marks info))
8607          (active (gnus-active (gnus-info-group info)))
8608          (min (car active))
8609          (max (cdr active))
8610          (types gnus-article-mark-lists)
8611          (uncompressed '(score bookmark))
8612          marks var articles article mark)
8613
8614     (while marked-lists
8615       (setq marks (pop marked-lists))
8616       (set (setq var (intern (format "gnus-newsgroup-%s"
8617                                      (car (rassq (setq mark (car marks))
8618                                                  types)))))
8619            (if (memq (car marks) uncompressed) (cdr marks)
8620              (gnus-uncompress-range (cdr marks))))
8621
8622       (setq articles (symbol-value var))
8623
8624       ;; All articles have to be subsets of the active articles.
8625       (cond
8626        ;; Adjust "simple" lists.
8627        ((memq mark '(tick dormant expirable reply killed save))
8628         (while articles
8629           (when (or (< (setq article (pop articles)) min) (> article max))
8630             (set var (delq article (symbol-value var))))))
8631        ;; Adjust assocs.
8632        ((memq mark '(score bookmark))
8633         (while articles
8634           (when (or (< (car (setq article (pop articles))) min)
8635                     (> (car article) max))
8636             (set var (delq article (symbol-value var))))))))))
8637
8638 (defun gnus-update-missing-marks (missing)
8639   "Go through the list of MISSING articles and remove them mark lists."
8640   (when missing
8641     (let ((types gnus-article-mark-lists)
8642           var m)
8643       ;; Go through all types.
8644       (while types
8645         (setq var (intern (format "gnus-newsgroup-%s" (car (pop types)))))
8646         (when (symbol-value var)
8647           ;; This list has articles.  So we delete all missing articles
8648           ;; from it.
8649           (setq m missing)
8650           (while m
8651             (set var (delq (pop m) (symbol-value var)))))))))
8652
8653 (defun gnus-update-marks ()
8654   "Enter the various lists of marked articles into the newsgroup info list."
8655   (let ((types gnus-article-mark-lists)
8656         (info (gnus-get-info gnus-newsgroup-name))
8657         (uncompressed '(score bookmark killed))
8658         var type list newmarked symbol)
8659     (when info
8660       ;; Add all marks lists that are non-nil to the list of marks lists.
8661       (while types
8662         (setq type (pop types))
8663         (when (setq list (symbol-value
8664                           (setq symbol
8665                                 (intern (format "gnus-newsgroup-%s"
8666                                                 (car type))))))
8667           (push (cons (cdr type)
8668                       (if (memq (cdr type) uncompressed) list
8669                         (gnus-compress-sequence (set symbol (sort list '<)) t)))
8670                 newmarked)))
8671
8672       ;; Enter these new marks into the info of the group.
8673       (if (nthcdr 3 info)
8674           (setcar (nthcdr 3 info) newmarked)
8675         ;; Add the marks lists to the end of the info.
8676         (when newmarked
8677           (setcdr (nthcdr 2 info) (list newmarked))))
8678
8679       ;; Cut off the end of the info if there's nothing else there.
8680       (let ((i 5))
8681         (while (and (> i 2)
8682                     (not (nth i info)))
8683           (when (nthcdr (decf i) info)
8684             (setcdr (nthcdr i info) nil)))))))
8685
8686 (defun gnus-add-marked-articles (group type articles &optional info force)
8687   ;; Add ARTICLES of TYPE to the info of GROUP.
8688   ;; If INFO is non-nil, use that info.  If FORCE is non-nil, don't
8689   ;; add, but replace marked articles of TYPE with ARTICLES.
8690   (let ((info (or info (gnus-get-info group)))
8691         (uncompressed '(score bookmark killed))
8692         marked m)
8693     (or (not info)
8694         (and (not (setq marked (nthcdr 3 info)))
8695              (or (null articles)
8696                  (setcdr (nthcdr 2 info)
8697                          (list (list (cons type (gnus-compress-sequence
8698                                                  articles t)))))))
8699         (and (not (setq m (assq type (car marked))))
8700              (or (null articles)
8701                  (setcar marked
8702                          (cons (cons type (gnus-compress-sequence articles t) )
8703                                (car marked)))))
8704         (if force
8705             (if (null articles)
8706                 (setcar (nthcdr 3 info)
8707                         (delq (assq type (car marked)) (car marked)))
8708               (setcdr m (gnus-compress-sequence articles t)))
8709           (setcdr m (gnus-compress-sequence
8710                      (sort (nconc (gnus-uncompress-range m)
8711                                   (copy-sequence articles)) '<) t))))))
8712
8713 (defun gnus-set-mode-line (where)
8714   "This function sets the mode line of the article or summary buffers.
8715 If WHERE is `summary', the summary mode line format will be used."
8716   ;; Is this mode line one we keep updated?
8717   (when (memq where gnus-updated-mode-lines)
8718     (let (mode-string)
8719       (save-excursion
8720         ;; We evaluate this in the summary buffer since these
8721         ;; variables are buffer-local to that buffer.
8722         (set-buffer gnus-summary-buffer)
8723         ;; We bind all these variables that are used in the `eval' form
8724         ;; below.
8725         (let* ((mformat (symbol-value
8726                          (intern
8727                           (format "gnus-%s-mode-line-format-spec" where))))
8728                (gnus-tmp-group-name gnus-newsgroup-name)
8729                (gnus-tmp-article-number (or gnus-current-article 0))
8730                (gnus-tmp-unread gnus-newsgroup-unreads)
8731                (gnus-tmp-unread-and-unticked (length gnus-newsgroup-unreads))
8732                (gnus-tmp-unselected (length gnus-newsgroup-unselected))
8733                (gnus-tmp-unread-and-unselected
8734                 (cond ((and (zerop gnus-tmp-unread-and-unticked)
8735                             (zerop gnus-tmp-unselected)) "")
8736                       ((zerop gnus-tmp-unselected)
8737                        (format "{%d more}" gnus-tmp-unread-and-unticked))
8738                       (t (format "{%d(+%d) more}"
8739                                  gnus-tmp-unread-and-unticked
8740                                  gnus-tmp-unselected))))
8741                (gnus-tmp-subject
8742                 (if (and gnus-current-headers
8743                          (vectorp gnus-current-headers))
8744                     (mail-header-subject gnus-current-headers) ""))
8745                max-len
8746                gnus-tmp-header);; passed as argument to any user-format-funcs
8747           (setq mode-string (eval mformat))
8748           (setq max-len (max 4 (if gnus-mode-non-string-length
8749                                    (- (frame-width)
8750                                       gnus-mode-non-string-length)
8751                                  (length mode-string))))
8752           ;; We might have to chop a bit of the string off...
8753           (when (> (length mode-string) max-len)
8754             (setq mode-string
8755                   (concat (gnus-truncate-string mode-string (- max-len 3))
8756                           "...")))
8757           ;; Pad the mode string a bit.
8758           (setq mode-string (format (format "%%-%ds" max-len) mode-string))))
8759       ;; Update the mode line.
8760       (setq mode-line-buffer-identification (list mode-string))
8761       (set-buffer-modified-p t))))
8762
8763 (defun gnus-create-xref-hashtb (from-newsgroup headers unreads)
8764   "Go through the HEADERS list and add all Xrefs to a hash table.
8765 The resulting hash table is returned, or nil if no Xrefs were found."
8766   (let* ((from-method (gnus-find-method-for-group from-newsgroup))
8767          (virtual (gnus-virtual-group-p from-newsgroup))
8768          (prefix (if virtual "" (gnus-group-real-prefix from-newsgroup)))
8769          (xref-hashtb (make-vector 63 0))
8770          start group entry number xrefs header)
8771     (while headers
8772       (setq header (pop headers))
8773       (when (and (setq xrefs (mail-header-xref header))
8774                  (not (memq (setq number (mail-header-number header))
8775                             unreads)))
8776         (setq start 0)
8777         (while (string-match "\\([^ ]+\\)[:/]\\([0-9]+\\)" xrefs start)
8778           (setq start (match-end 0))
8779           (setq group (concat prefix (substring xrefs (match-beginning 1)
8780                                                 (match-end 1))))
8781           (setq number
8782                 (string-to-int (substring xrefs (match-beginning 2)
8783                                           (match-end 2))))
8784           (if (setq entry (gnus-gethash group xref-hashtb))
8785               (setcdr entry (cons number (cdr entry)))
8786             (gnus-sethash group (cons number nil) xref-hashtb)))))
8787     (and start xref-hashtb)))
8788
8789 (defun gnus-mark-xrefs-as-read (from-newsgroup headers unreads)
8790   "Look through all the headers and mark the Xrefs as read."
8791   (let ((virtual (gnus-virtual-group-p from-newsgroup))
8792         name entry info xref-hashtb idlist method nth4)
8793     (save-excursion
8794       (set-buffer gnus-group-buffer)
8795       (when (setq xref-hashtb
8796                   (gnus-create-xref-hashtb from-newsgroup headers unreads))
8797         (mapatoms
8798          (lambda (group)
8799            (unless (string= from-newsgroup (setq name (symbol-name group)))
8800              (setq idlist (symbol-value group))
8801              ;; Dead groups are not updated.
8802              (and (prog1
8803                       (setq entry (gnus-gethash name gnus-newsrc-hashtb)
8804                             info (nth 2 entry))
8805                     (if (stringp (setq nth4 (gnus-info-method info)))
8806                         (setq nth4 (gnus-server-to-method nth4))))
8807                   ;; Only do the xrefs if the group has the same
8808                   ;; select method as the group we have just read.
8809                   (or (gnus-methods-equal-p
8810                        nth4 (gnus-find-method-for-group from-newsgroup))
8811                       virtual
8812                       (equal nth4 (setq method (gnus-find-method-for-group
8813                                                 from-newsgroup)))
8814                       (and (equal (car nth4) (car method))
8815                            (equal (nth 1 nth4) (nth 1 method))))
8816                   gnus-use-cross-reference
8817                   (or (not (eq gnus-use-cross-reference t))
8818                       virtual
8819                       ;; Only do cross-references on subscribed
8820                       ;; groups, if that is what is wanted.
8821                       (<= (gnus-info-level info) gnus-level-subscribed))
8822                   (gnus-group-make-articles-read name idlist))))
8823          xref-hashtb)))))
8824
8825 (defun gnus-group-make-articles-read (group articles)
8826   (let* ((num 0)
8827          (entry (gnus-gethash group gnus-newsrc-hashtb))
8828          (info (nth 2 entry))
8829          (active (gnus-active group))
8830          range)
8831     ;; First peel off all illegal article numbers.
8832     (if active
8833         (let ((ids articles)
8834               id first)
8835           (while ids
8836             (setq id (car ids))
8837             (if (and first (> id (cdr active)))
8838                 (progn
8839                   ;; We'll end up in this situation in one particular
8840                   ;; obscure situation.  If you re-scan a group and get
8841                   ;; a new article that is cross-posted to a different
8842                   ;; group that has not been re-scanned, you might get
8843                   ;; crossposted article that has a higher number than
8844                   ;; Gnus believes possible.  So we re-activate this
8845                   ;; group as well.  This might mean doing the
8846                   ;; crossposting thingy will *increase* the number
8847                   ;; of articles in some groups.  Tsk, tsk.
8848                   (setq active (or (gnus-activate-group group) active))))
8849             (if (or (> id (cdr active))
8850                     (< id (car active)))
8851                 (setq articles (delq id articles)))
8852             (setq ids (cdr ids)))))
8853     ;; If the read list is nil, we init it.
8854     (and active
8855          (null (gnus-info-read info))
8856          (> (car active) 1)
8857          (gnus-info-set-read info (cons 1 (1- (car active)))))
8858     ;; Then we add the read articles to the range.
8859     (gnus-info-set-read
8860      info
8861      (setq range
8862            (gnus-add-to-range
8863             (gnus-info-read info) (setq articles (sort articles '<)))))
8864     ;; Then we have to re-compute how many unread
8865     ;; articles there are in this group.
8866     (if active
8867         (progn
8868           (cond
8869            ((not range)
8870             (setq num (- (1+ (cdr active)) (car active))))
8871            ((not (listp (cdr range)))
8872             (setq num (- (cdr active) (- (1+ (cdr range))
8873                                          (car range)))))
8874            (t
8875             (while range
8876               (if (numberp (car range))
8877                   (setq num (1+ num))
8878                 (setq num (+ num (- (1+ (cdar range)) (caar range)))))
8879               (setq range (cdr range)))
8880             (setq num (- (cdr active) num))))
8881           ;; Update the number of unread articles.
8882           (setcar entry num)
8883           ;; Update the group buffer.
8884           (gnus-group-update-group group t)))))
8885
8886 (defun gnus-methods-equal-p (m1 m2)
8887   (let ((m1 (or m1 gnus-select-method))
8888         (m2 (or m2 gnus-select-method)))
8889     (or (equal m1 m2)
8890         (and (eq (car m1) (car m2))
8891              (or (not (memq 'address (assoc (symbol-name (car m1))
8892                                             gnus-valid-select-methods)))
8893                  (equal (nth 1 m1) (nth 1 m2)))))))
8894
8895 (defsubst gnus-header-value ()
8896   (buffer-substring (match-end 0) (gnus-point-at-eol)))
8897
8898 (defvar gnus-newsgroup-none-id 0)
8899
8900 (defun gnus-get-newsgroup-headers (&optional dependencies force-new)
8901   (let ((cur nntp-server-buffer)
8902         (dependencies
8903          (or dependencies
8904              (save-excursion (set-buffer gnus-summary-buffer)
8905                              gnus-newsgroup-dependencies)))
8906         headers id id-dep ref-dep end ref)
8907     (save-excursion
8908       (set-buffer nntp-server-buffer)
8909       (let ((case-fold-search t)
8910             in-reply-to header number p lines)
8911         (goto-char (point-min))
8912         ;; Search to the beginning of the next header.  Error messages
8913         ;; do not begin with 2 or 3.
8914         (while (re-search-forward "^[23][0-9]+ " nil t)
8915           (setq id nil
8916                 ref nil)
8917           ;; This implementation of this function, with nine
8918           ;; search-forwards instead of the one re-search-forward and
8919           ;; a case (which basically was the old function) is actually
8920           ;; about twice as fast, even though it looks messier.  You
8921           ;; can't have everything, I guess.  Speed and elegance
8922           ;; doesn't always go hand in hand.
8923           (setq
8924            header
8925            (vector
8926             ;; Number.
8927             (prog1
8928                 (read cur)
8929               (end-of-line)
8930               (setq p (point))
8931               (narrow-to-region (point)
8932                                 (or (and (search-forward "\n.\n" nil t)
8933                                          (- (point) 2))
8934                                     (point))))
8935             ;; Subject.
8936             (progn
8937               (goto-char p)
8938               (if (search-forward "\nsubject: " nil t)
8939                   (gnus-header-value) "(none)"))
8940             ;; From.
8941             (progn
8942               (goto-char p)
8943               (if (search-forward "\nfrom: " nil t)
8944                   (gnus-header-value) "(nobody)"))
8945             ;; Date.
8946             (progn
8947               (goto-char p)
8948               (if (search-forward "\ndate: " nil t)
8949                   (gnus-header-value) ""))
8950             ;; Message-ID.
8951             (progn
8952               (goto-char p)
8953               (if (search-forward "\nmessage-id: " nil t)
8954                   (setq id (gnus-header-value))
8955                 ;; If there was no message-id, we just fake one to make
8956                 ;; subsequent routines simpler.
8957                 (setq id (concat "none+"
8958                                  (int-to-string
8959                                   (setq gnus-newsgroup-none-id
8960                                         (1+ gnus-newsgroup-none-id)))))))
8961             ;; References.
8962             (progn
8963               (goto-char p)
8964               (if (search-forward "\nreferences: " nil t)
8965                   (prog1
8966                       (gnus-header-value)
8967                     (setq end (match-end 0))
8968                     (save-excursion
8969                       (setq ref
8970                             (downcase
8971                              (buffer-substring
8972                               (progn
8973                                 (end-of-line)
8974                                 (search-backward ">" end t)
8975                                 (1+ (point)))
8976                               (progn
8977                                 (search-backward "<" end t)
8978                                 (point)))))))
8979                 ;; Get the references from the in-reply-to header if there
8980                 ;; were no references and the in-reply-to header looks
8981                 ;; promising.
8982                 (if (and (search-forward "\nin-reply-to: " nil t)
8983                          (setq in-reply-to (gnus-header-value))
8984                          (string-match "<[^>]+>" in-reply-to))
8985                     (prog1
8986                         (setq ref (substring in-reply-to (match-beginning 0)
8987                                              (match-end 0)))
8988                       (setq ref (downcase ref))))
8989                 (setq ref "")))
8990             ;; Chars.
8991             0
8992             ;; Lines.
8993             (progn
8994               (goto-char p)
8995               (if (search-forward "\nlines: " nil t)
8996                   (if (numberp (setq lines (read cur)))
8997                       lines 0)
8998                 0))
8999             ;; Xref.
9000             (progn
9001               (goto-char p)
9002               (and (search-forward "\nxref: " nil t)
9003                    (gnus-header-value)))))
9004           ;; We do the threading while we read the headers.  The
9005           ;; message-id and the last reference are both entered into
9006           ;; the same hash table.  Some tippy-toeing around has to be
9007           ;; done in case an article has arrived before the article
9008           ;; which it refers to.
9009           (if (boundp (setq id-dep (intern (downcase id) dependencies)))
9010               (if (and (car (symbol-value id-dep))
9011                        (not force-new))
9012                   ;; An article with this Message-ID has already
9013                   ;; been seen, so we ignore this one, except we add
9014                   ;; any additional Xrefs (in case the two articles
9015                   ;; came from different servers).
9016                   (progn
9017                     (mail-header-set-xref
9018                      (car (symbol-value id-dep))
9019                      (concat (or (mail-header-xref
9020                                   (car (symbol-value id-dep))) "")
9021                              (or (mail-header-xref header) "")))
9022                     (setq header nil))
9023                 (setcar (symbol-value id-dep) header))
9024             (set id-dep (list header)))
9025           (when header
9026             (if (boundp (setq ref-dep (intern ref dependencies)))
9027                 (setcdr (symbol-value ref-dep)
9028                         (nconc (cdr (symbol-value ref-dep))
9029                                (list (symbol-value id-dep))))
9030               (set ref-dep (list nil (symbol-value id-dep))))
9031             (setq headers (cons header headers)))
9032           (goto-char (point-max))
9033           (widen))
9034         (nreverse headers)))))
9035
9036 ;; The following macros and functions were written by Felix Lee
9037 ;; <flee@cse.psu.edu>.
9038
9039 (defmacro gnus-nov-read-integer ()
9040   '(prog1
9041        (if (= (following-char) ?\t)
9042            0
9043          (let ((num (condition-case nil (read buffer) (error nil))))
9044            (if (numberp num) num 0)))
9045      (or (eobp) (forward-char 1))))
9046
9047 (defmacro gnus-nov-skip-field ()
9048   '(search-forward "\t" eol 'move))
9049
9050 (defmacro gnus-nov-field ()
9051   '(buffer-substring (point) (if (gnus-nov-skip-field) (1- (point)) eol)))
9052
9053 ;; Goes through the xover lines and returns a list of vectors
9054 (defun gnus-get-newsgroup-headers-xover (sequence &optional force-new)
9055   "Parse the news overview data in the server buffer, and return a
9056 list of headers that match SEQUENCE (see `nntp-retrieve-headers')."
9057   ;; Get the Xref when the users reads the articles since most/some
9058   ;; NNTP servers do not include Xrefs when using XOVER.
9059   (setq gnus-article-internal-prepare-hook '(gnus-article-get-xrefs))
9060   (let ((cur nntp-server-buffer)
9061         (dependencies gnus-newsgroup-dependencies)
9062         number headers header)
9063     (save-excursion
9064       (set-buffer nntp-server-buffer)
9065       ;; Allow the user to mangle the headers before parsing them.
9066       (run-hooks 'gnus-parse-headers-hook)
9067       ;; Allow the user to mangle the headers before parsing them.
9068       (run-hooks 'gnus-parse-headers-hook)
9069       (goto-char (point-min))
9070       (while (and sequence (not (eobp)))
9071         (setq number (read cur))
9072         (while (and sequence (< (car sequence) number))
9073           (setq sequence (cdr sequence)))
9074         (and sequence
9075              (eq number (car sequence))
9076              (progn
9077                (setq sequence (cdr sequence))
9078                (if (setq header
9079                          (inline (gnus-nov-parse-line
9080                                   number dependencies force-new)))
9081                    (setq headers (cons header headers)))))
9082         (forward-line 1))
9083       (setq headers (nreverse headers)))
9084     headers))
9085
9086 ;; This function has to be called with point after the article number
9087 ;; on the beginning of the line.
9088 (defun gnus-nov-parse-line (number dependencies &optional force-new)
9089   (let ((none 0)
9090         (eol (gnus-point-at-eol))
9091         (buffer (current-buffer))
9092         header ref id id-dep ref-dep)
9093
9094     ;; overview: [num subject from date id refs chars lines misc]
9095     (narrow-to-region (point) eol)
9096     (or (eobp) (forward-char))
9097
9098     (condition-case nil
9099         (setq header
9100               (vector
9101                number                   ; number
9102                (gnus-nov-field)         ; subject
9103                (gnus-nov-field)         ; from
9104                (gnus-nov-field)         ; date
9105                (setq id (or (gnus-nov-field)
9106                             (concat "none+"
9107                                     (int-to-string
9108                                      (setq none (1+ none)))))) ; id
9109                (progn
9110                  (save-excursion
9111                    (let ((beg (point)))
9112                      (search-forward "\t" eol)
9113                      (if (search-backward ">" beg t)
9114                          (setq ref
9115                                (downcase
9116                                 (buffer-substring
9117                                  (1+ (point))
9118                                  (progn
9119                                    (search-backward "<" beg t)
9120                                    (point)))))
9121                        (setq ref nil))))
9122                  (gnus-nov-field))      ; refs
9123                (gnus-nov-read-integer)  ; chars
9124                (gnus-nov-read-integer)  ; lines
9125                (if (= (following-char) ?\n)
9126                    nil
9127                  (gnus-nov-field))      ; misc
9128                ))
9129       (error (progn
9130                (ding)
9131                (gnus-message 4 "Strange nov line")
9132                (setq header nil)
9133                (goto-char eol))))
9134
9135     (widen)
9136
9137     ;; We build the thread tree.
9138     (when header
9139       (if (boundp (setq id-dep (intern (downcase id) dependencies)))
9140           (if (and (car (symbol-value id-dep))
9141                    (not force-new))
9142               ;; An article with this Message-ID has already been seen,
9143               ;; so we ignore this one, except we add any additional
9144               ;; Xrefs (in case the two articles came from different
9145               ;; servers.
9146               (progn
9147                 (mail-header-set-xref
9148                  (car (symbol-value id-dep))
9149                  (concat (or (mail-header-xref
9150                               (car (symbol-value id-dep))) "")
9151                          (or (mail-header-xref header) "")))
9152                 (setq header nil))
9153             (setcar (symbol-value id-dep) header))
9154         (set id-dep (list header))))
9155     (if header
9156         (progn
9157           (if (boundp (setq ref-dep (intern (or ref "none") dependencies)))
9158               (setcdr (symbol-value ref-dep)
9159                       (nconc (cdr (symbol-value ref-dep))
9160                              (list (symbol-value id-dep))))
9161             (set ref-dep (list nil (symbol-value id-dep))))))
9162     header))
9163
9164 (defun gnus-article-get-xrefs ()
9165   "Fill in the Xref value in `gnus-current-headers', if necessary.
9166 This is meant to be called in `gnus-article-internal-prepare-hook'."
9167   (let ((headers (save-excursion (set-buffer gnus-summary-buffer)
9168                                  gnus-current-headers)))
9169     (or (not gnus-use-cross-reference)
9170         (not headers)
9171         (and (mail-header-xref headers)
9172              (not (string= (mail-header-xref headers) "")))
9173         (let ((case-fold-search t)
9174               xref)
9175           (save-restriction
9176             (nnheader-narrow-to-headers)
9177             (goto-char (point-min))
9178             (if (or (and (eq (downcase (following-char)) ?x)
9179                          (looking-at "Xref:"))
9180                     (search-forward "\nXref:" nil t))
9181                 (progn
9182                   (goto-char (1+ (match-end 0)))
9183                   (setq xref (buffer-substring (point)
9184                                                (progn (end-of-line) (point))))
9185                   (mail-header-set-xref headers xref))))))))
9186
9187 (defun gnus-summary-insert-subject (id)
9188   "Find article ID and insert the summary line for that article."
9189   (let ((header (gnus-read-header id))
9190         (number (and (numberp id) id)))
9191     (when header
9192       ;; Rebuild the thread that this article is part of and go to the
9193       ;; article we have fetched.
9194       (gnus-rebuild-thread (mail-header-id header))
9195       (gnus-summary-goto-subject (setq number (mail-header-number header))))
9196     (when (and (numberp number)
9197                (> number 0))
9198       ;; We have to update the boundaries even if we can't fetch the
9199       ;; article if ID is a number -- so that the next `P' or `N'
9200       ;; command will fetch the previous (or next) article even
9201       ;; if the one we tried to fetch this time has been canceled.
9202       (and (> number gnus-newsgroup-end)
9203            (setq gnus-newsgroup-end number))
9204       (and (< number gnus-newsgroup-begin)
9205            (setq gnus-newsgroup-begin number))
9206       (setq gnus-newsgroup-unselected
9207             (delq number gnus-newsgroup-unselected)))
9208     ;; Report back a success?
9209     (and header number)))
9210
9211 (defun gnus-summary-work-articles (n)
9212   "Return a list of articles to be worked upon.  The prefix argument,
9213 the list of process marked articles, and the current article will be
9214 taken into consideration."
9215   (cond
9216    ((and n (numberp n))
9217     ;; A numerical prefix has been given.
9218     (let ((backward (< n 0))
9219           (n (abs n))
9220           articles article)
9221       (save-excursion
9222         (while
9223             (and (> n 0)
9224                  (push (setq article (gnus-summary-article-number))
9225                        articles)
9226                  (if backward
9227                      (gnus-summary-find-prev nil article)
9228                    (gnus-summary-find-next nil article)))
9229           (decf n)))
9230       (nreverse articles)))
9231    ((and (boundp 'transient-mark-mode)
9232          transient-mark-mode
9233          mark-active)
9234     ;; Work on the region between point and mark.
9235     (let ((max (max (point) (mark)))
9236           articles article)
9237       (save-excursion
9238         (goto-char (min (point) (mark)))
9239         (while
9240             (and
9241              (push (setq article (gnus-summary-article-number)) articles)
9242              (gnus-summary-find-next nil article)
9243              (< (point) max)))
9244         (nreverse articles))))
9245    (gnus-newsgroup-processable
9246     ;; There are process-marked articles present.
9247     (reverse gnus-newsgroup-processable))
9248    (t
9249     ;; Just return the current article.
9250     (list (gnus-summary-article-number)))))
9251
9252 (defun gnus-summary-search-group (&optional backward use-level)
9253   "Search for next unread newsgroup.
9254 If optional argument BACKWARD is non-nil, search backward instead."
9255   (save-excursion
9256     (set-buffer gnus-group-buffer)
9257     (if (gnus-group-search-forward
9258          backward nil (if use-level (gnus-group-group-level) nil))
9259         (gnus-group-group-name))))
9260
9261 (defun gnus-summary-best-group (&optional exclude-group)
9262   "Find the name of the best unread group.
9263 If EXCLUDE-GROUP, do not go to this group."
9264   (save-excursion
9265     (set-buffer gnus-group-buffer)
9266     (save-excursion
9267       (gnus-group-best-unread-group exclude-group))))
9268
9269 (defun gnus-summary-find-next (&optional unread article backward)
9270   (if backward (gnus-summary-find-prev)
9271     (let* ((article (or article (gnus-summary-article-number)))
9272            (arts (gnus-data-find-list article))
9273            result)
9274       (when (or (not gnus-summary-check-current)
9275                 (not unread)
9276                 (not (gnus-data-unread-p (car arts))))
9277         (setq arts (cdr arts)))
9278       (when (setq result
9279                   (if unread
9280                       (progn
9281                         (while arts
9282                           (when (gnus-data-unread-p (car arts))
9283                             (setq result (car arts)
9284                                   arts nil))
9285                           (setq arts (cdr arts)))
9286                         result)
9287                     (car arts)))
9288         (goto-char (gnus-data-pos result))
9289         (gnus-data-number result)))))
9290
9291 (defun gnus-summary-find-prev (&optional unread article)
9292   (let* ((article (or article (gnus-summary-article-number)))
9293          (arts (gnus-data-find-list article (gnus-data-list 'rev)))
9294          result)
9295     (when (or (not gnus-summary-check-current)
9296               (not unread)
9297               (not (gnus-data-unread-p (car arts))))
9298       (setq arts (cdr arts)))
9299     (if (setq result
9300               (if unread
9301                   (progn
9302                     (while arts
9303                       (and (gnus-data-unread-p (car arts))
9304                            (setq result (car arts)
9305                                  arts nil))
9306                       (setq arts (cdr arts)))
9307                     result)
9308                 (car arts)))
9309         (progn
9310           (goto-char (gnus-data-pos result))
9311           (gnus-data-number result)))))
9312
9313 (defun gnus-summary-find-subject (subject &optional unread backward article)
9314   (let* ((simp-subject (gnus-simplify-subject-fully subject))
9315          (article (or article (gnus-summary-article-number)))
9316          (articles (gnus-data-list backward))
9317          (arts (gnus-data-find-list article articles))
9318          result)
9319     (when (or (not gnus-summary-check-current)
9320               (not unread)
9321               (not (gnus-data-unread-p (car arts))))
9322       (setq arts (cdr arts)))
9323     (while arts
9324       (and (or (not unread)
9325                (gnus-data-unread-p (car arts)))
9326            (vectorp (gnus-data-header (car arts)))
9327            (gnus-subject-equal
9328             simp-subject (mail-header-subject (gnus-data-header (car arts))) t)
9329            (setq result (car arts)
9330                  arts nil))
9331       (setq arts (cdr arts)))
9332     (and result
9333          (goto-char (gnus-data-pos result))
9334          (gnus-data-number result))))
9335
9336 (defun gnus-summary-search-forward (&optional unread subject backward)
9337   "Search forward for an article.
9338 If UNREAD, look for unread articles.  If SUBJECT, look for
9339 articles with that subject.  If BACKWARD, search backward instead."
9340   (cond (subject (gnus-summary-find-subject subject unread backward))
9341         (backward (gnus-summary-find-prev unread))
9342         (t (gnus-summary-find-next unread))))
9343
9344 (defun gnus-recenter (&optional n)
9345   "Center point in window and redisplay frame.
9346 Also do horizontal recentering."
9347   (interactive "P")
9348   (when (and gnus-auto-center-summary
9349              (not (eq gnus-auto-center-summary 'vertical)))
9350     (gnus-horizontal-recenter))
9351   (recenter n))
9352
9353 (defun gnus-summary-recenter ()
9354   "Center point in the summary window.
9355 If `gnus-auto-center-summary' is nil, or the article buffer isn't
9356 displayed, no centering will be performed."
9357   ;; Suggested by earle@mahendo.JPL.NASA.GOV (Greg Earle).
9358   ;; Recenter only when requested.  Suggested by popovich@park.cs.columbia.edu.
9359   (let* ((top (cond ((< (window-height) 4) 0)
9360                     ((< (window-height) 7) 1)
9361                     (t 2)))
9362          (height (1- (window-height)))
9363          (bottom (save-excursion (goto-char (point-max))
9364                                  (forward-line (- height))
9365                                  (point)))
9366          (window (get-buffer-window (current-buffer))))
9367     ;; The user has to want it.
9368     (when gnus-auto-center-summary
9369       (when (get-buffer-window gnus-article-buffer)
9370        ;; Only do recentering when the article buffer is displayed,
9371        ;; Set the window start to either `bottom', which is the biggest
9372        ;; possible valid number, or the second line from the top,
9373        ;; whichever is the least.
9374        (set-window-start
9375         window (min bottom (save-excursion 
9376                              (forward-line (- top)) (point)))))
9377       ;; Do horizontal recentering while we're at it.
9378       (when (and (get-buffer-window (current-buffer) t)
9379                  (not (eq gnus-auto-center-summary 'vertical)))
9380         (let ((selected (selected-window)))
9381           (select-window (get-buffer-window (current-buffer) t))
9382           (gnus-summary-position-point)
9383           (gnus-horizontal-recenter)
9384           (select-window selected))))))
9385
9386 (defun gnus-horizontal-recenter ()
9387   "Recenter the current buffer horizontally."
9388   (if (< (current-column) (/ (window-width) 2))
9389       (set-window-hscroll (get-buffer-window (current-buffer) t) 0)
9390     (let* ((orig (point))
9391            (end (window-end (get-buffer-window (current-buffer) t)))
9392            (max 0))
9393       ;; Find the longest line currently displayed in the window.
9394       (goto-char (window-start))
9395       (while (and (not (eobp)) 
9396                   (< (point) end))
9397         (end-of-line)
9398         (setq max (max max (current-column)))
9399         (forward-line 1))
9400       (goto-char orig)
9401       ;; Scroll horizontally to center (sort of) the point.
9402       (if (> max (window-width))
9403           (set-window-hscroll 
9404            (get-buffer-window (current-buffer) t)
9405            (min (- (current-column) (/ (window-width) 3))
9406                 (+ 2 (- max (window-width)))))
9407         (set-window-hscroll (get-buffer-window (current-buffer) t) 0))
9408       max)))
9409
9410 ;; Function written by Stainless Steel Rat <ratinox@ccs.neu.edu>.
9411 (defun gnus-short-group-name (group &optional levels)
9412   "Collapse GROUP name LEVELS."
9413   (let* ((name "") 
9414          (foreign "")
9415          (depth 0) 
9416          (skip 1)
9417          (levels (or levels
9418                      (progn
9419                        (while (string-match "\\." group skip)
9420                          (setq skip (match-end 0)
9421                                depth (+ depth 1)))
9422                        depth))))
9423     (if (string-match ":" group)
9424         (setq foreign (substring group 0 (match-end 0))
9425               group (substring group (match-end 0))))
9426     (while group
9427       (if (and (string-match "\\." group)
9428                (> levels (- gnus-group-uncollapsed-levels 1)))
9429           (setq name (concat name (substring group 0 1))
9430                 group (substring group (match-end 0))
9431                 levels (- levels 1)
9432                 name (concat name "."))
9433         (setq name (concat foreign name group)
9434               group nil)))
9435     name))
9436
9437 (defun gnus-summary-jump-to-group (newsgroup)
9438   "Move point to NEWSGROUP in group mode buffer."
9439   ;; Keep update point of group mode buffer if visible.
9440   (if (eq (current-buffer) (get-buffer gnus-group-buffer))
9441       (save-window-excursion
9442         ;; Take care of tree window mode.
9443         (if (get-buffer-window gnus-group-buffer)
9444             (pop-to-buffer gnus-group-buffer))
9445         (gnus-group-jump-to-group newsgroup))
9446     (save-excursion
9447       ;; Take care of tree window mode.
9448       (if (get-buffer-window gnus-group-buffer)
9449           (pop-to-buffer gnus-group-buffer)
9450         (set-buffer gnus-group-buffer))
9451       (gnus-group-jump-to-group newsgroup))))
9452
9453 ;; This function returns a list of article numbers based on the
9454 ;; difference between the ranges of read articles in this group and
9455 ;; the range of active articles.
9456 (defun gnus-list-of-unread-articles (group)
9457   (let* ((read (gnus-info-read (gnus-get-info group)))
9458          (active (gnus-active group))
9459          (last (cdr active))
9460          first nlast unread)
9461     ;; If none are read, then all are unread.
9462     (if (not read)
9463         (setq first (car active))
9464       ;; If the range of read articles is a single range, then the
9465       ;; first unread article is the article after the last read
9466       ;; article.  Sounds logical, doesn't it?
9467       (if (not (listp (cdr read)))
9468           (setq first (1+ (cdr read)))
9469         ;; `read' is a list of ranges.
9470         (if (/= (setq nlast (or (and (numberp (car read)) (car read))
9471                                 (caar read))) 1)
9472             (setq first 1))
9473         (while read
9474           (if first
9475               (while (< first nlast)
9476                 (setq unread (cons first unread))
9477                 (setq first (1+ first))))
9478           (setq first (1+ (if (atom (car read)) (car read) (cdar read))))
9479           (setq nlast (if (atom (cadr read)) (cadr read) (caadr read)))
9480           (setq read (cdr read)))))
9481     ;; And add the last unread articles.
9482     (while (<= first last)
9483       (setq unread (cons first unread))
9484       (setq first (1+ first)))
9485     ;; Return the list of unread articles.
9486     (nreverse unread)))
9487
9488 (defun gnus-list-of-read-articles (group)
9489   "Return a list of unread, unticked and non-dormant articles."
9490   (let* ((info (gnus-get-info group))
9491          (marked (gnus-info-marks info))
9492          (active (gnus-active group)))
9493     (and info active
9494          (gnus-set-difference
9495           (gnus-sorted-complement
9496            (gnus-uncompress-range active)
9497            (gnus-list-of-unread-articles group))
9498           (append
9499            (gnus-uncompress-range (cdr (assq 'dormant marked)))
9500            (gnus-uncompress-range (cdr (assq 'tick marked))))))))
9501
9502 ;; Various summary commands
9503
9504 (defun gnus-summary-universal-argument (arg)
9505   "Perform any operation on all articles that are process/prefixed."
9506   (interactive "P")
9507   (gnus-set-global-variables)
9508   (let ((articles (gnus-summary-work-articles arg))
9509         func article)
9510     (if (eq
9511          (setq
9512           func
9513           (key-binding
9514            (read-key-sequence
9515             (substitute-command-keys
9516              "\\<gnus-summary-mode-map>\\[gnus-summary-universal-argument]"
9517              ))))
9518          'undefined)
9519         (progn
9520           (message "Undefined key")
9521           (ding))
9522       (save-excursion
9523         (while articles
9524           (gnus-summary-goto-subject (setq article (pop articles)))
9525           (command-execute func)
9526           (gnus-summary-remove-process-mark article)))))
9527   (gnus-summary-position-point))
9528
9529 (defun gnus-summary-toggle-truncation (&optional arg)
9530   "Toggle truncation of summary lines.
9531 With arg, turn line truncation on iff arg is positive."
9532   (interactive "P")
9533   (setq truncate-lines
9534         (if (null arg) (not truncate-lines)
9535           (> (prefix-numeric-value arg) 0)))
9536   (redraw-display))
9537
9538 (defun gnus-summary-reselect-current-group (&optional all rescan)
9539   "Exit and then reselect the current newsgroup.
9540 The prefix argument ALL means to select all articles."
9541   (interactive "P")
9542   (gnus-set-global-variables)
9543   (let ((current-subject (gnus-summary-article-number))
9544         (group gnus-newsgroup-name))
9545     (setq gnus-newsgroup-begin nil)
9546     (gnus-summary-exit)
9547     ;; We have to adjust the point of group mode buffer because the
9548     ;; current point was moved to the next unread newsgroup by
9549     ;; exiting.
9550     (gnus-summary-jump-to-group group)
9551     (when rescan
9552       (save-excursion
9553         (gnus-group-get-new-news-this-group 1)))
9554     (gnus-group-read-group all t)
9555     (gnus-summary-goto-subject current-subject)))
9556
9557 (defun gnus-summary-rescan-group (&optional all)
9558   "Exit the newsgroup, ask for new articles, and select the newsgroup."
9559   (interactive "P")
9560   (gnus-summary-reselect-current-group all t))
9561
9562 (defun gnus-summary-update-info ()
9563   (let* ((group gnus-newsgroup-name))
9564     (when gnus-newsgroup-kill-headers
9565       (setq gnus-newsgroup-killed
9566             (gnus-compress-sequence
9567              (nconc
9568               (gnus-set-sorted-intersection
9569                (gnus-uncompress-range gnus-newsgroup-killed)
9570                (setq gnus-newsgroup-unselected
9571                      (sort gnus-newsgroup-unselected '<)))
9572               (setq gnus-newsgroup-unreads
9573                     (sort gnus-newsgroup-unreads '<))) t)))
9574     (unless (listp (cdr gnus-newsgroup-killed))
9575       (setq gnus-newsgroup-killed (list gnus-newsgroup-killed)))
9576     (let ((headers gnus-newsgroup-headers))
9577       (gnus-close-group group)
9578       (run-hooks 'gnus-exit-group-hook)
9579       (unless gnus-save-score
9580         (setq gnus-newsgroup-scored nil))
9581       ;; Set the new ranges of read articles.
9582       (gnus-update-read-articles
9583        group (append gnus-newsgroup-unreads gnus-newsgroup-unselected))
9584       ;; Set the current article marks.
9585       (gnus-update-marks)
9586       ;; Do the cross-ref thing.
9587       (when gnus-use-cross-reference
9588         (gnus-mark-xrefs-as-read group headers gnus-newsgroup-unreads))
9589       ;; Do adaptive scoring, and possibly save score files.
9590       (when gnus-newsgroup-adaptive
9591         (gnus-score-adaptive))
9592       (when gnus-use-scoring
9593         (gnus-score-save))
9594       ;; Do not switch windows but change the buffer to work.
9595       (set-buffer gnus-group-buffer)
9596       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
9597           (gnus-group-update-group group)))))
9598
9599 (defun gnus-summary-exit (&optional temporary)
9600   "Exit reading current newsgroup, and then return to group selection mode.
9601 gnus-exit-group-hook is called with no arguments if that value is non-nil."
9602   (interactive)
9603   (gnus-set-global-variables)
9604   (gnus-kill-save-kill-buffer)
9605   (let* ((group gnus-newsgroup-name)
9606          (quit-config (gnus-group-quit-config gnus-newsgroup-name))
9607          (mode major-mode)
9608          (buf (current-buffer)))
9609     (run-hooks 'gnus-summary-prepare-exit-hook)
9610     ;; If we have several article buffers, we kill them at exit.
9611     (unless gnus-single-article-buffer
9612       (gnus-kill-buffer gnus-article-buffer)
9613       (gnus-kill-buffer gnus-original-article-buffer))
9614     (when gnus-use-cache
9615       (gnus-cache-possibly-remove-articles)
9616       (gnus-cache-save-buffers))
9617     (when gnus-use-trees
9618       (gnus-tree-close group))
9619     ;; Make all changes in this group permanent.
9620     (unless quit-config
9621       (gnus-summary-update-info))
9622     ;; Make sure where I was, and go to next newsgroup.
9623     (set-buffer gnus-group-buffer)
9624     (unless quit-config
9625       (gnus-group-jump-to-group group)
9626       (gnus-group-next-unread-group 1))
9627     (run-hooks 'gnus-summary-exit-hook)
9628     (if temporary
9629         nil                             ;Nothing to do.
9630       ;; If we have several article buffers, we kill them at exit.
9631       (unless gnus-single-article-buffer
9632         (gnus-kill-buffer gnus-article-buffer)
9633         (gnus-kill-buffer gnus-original-article-buffer))
9634       (set-buffer buf)
9635       (if (not gnus-kill-summary-on-exit)
9636           (gnus-deaden-summary)
9637         ;; We set all buffer-local variables to nil.  It is unclear why
9638         ;; this is needed, but if we don't, buffer-local variables are
9639         ;; not garbage-collected, it seems.  This would the lead to en
9640         ;; ever-growing Emacs.
9641         (gnus-summary-clear-local-variables)
9642         (when (get-buffer gnus-article-buffer)
9643           (bury-buffer gnus-article-buffer))
9644         ;; We clear the global counterparts of the buffer-local
9645         ;; variables as well, just to be on the safe side.
9646         (gnus-configure-windows 'group 'force)
9647         (gnus-summary-clear-local-variables)
9648         ;; Return to group mode buffer.
9649         (if (eq mode 'gnus-summary-mode)
9650             (gnus-kill-buffer buf)))
9651       (setq gnus-current-select-method gnus-select-method)
9652       (pop-to-buffer gnus-group-buffer)
9653       ;; Clear the current group name.
9654       (if (not quit-config)
9655           (progn
9656             (gnus-group-jump-to-group group)
9657             (gnus-group-next-unread-group 1)
9658             (gnus-configure-windows 'group 'force))
9659         (if (not (buffer-name (car quit-config)))
9660             (gnus-configure-windows 'group 'force)
9661           (set-buffer (car quit-config))
9662           (and (eq major-mode 'gnus-summary-mode)
9663                (gnus-set-global-variables))
9664           (gnus-configure-windows (cdr quit-config))))
9665       (unless quit-config
9666         (setq gnus-newsgroup-name nil)))))
9667
9668 (defalias 'gnus-summary-quit 'gnus-summary-exit-no-update)
9669 (defun gnus-summary-exit-no-update (&optional no-questions)
9670   "Quit reading current newsgroup without updating read article info."
9671   (interactive)
9672   (gnus-set-global-variables)
9673   (let* ((group gnus-newsgroup-name)
9674          (quit-config (gnus-group-quit-config group)))
9675     (when (or no-questions
9676               gnus-expert-user
9677               (gnus-y-or-n-p "Do you really wanna quit reading this group? "))
9678       ;; If we have several article buffers, we kill them at exit.
9679       (unless gnus-single-article-buffer
9680         (gnus-kill-buffer gnus-article-buffer)
9681         (gnus-kill-buffer gnus-original-article-buffer))
9682       (if (not gnus-kill-summary-on-exit)
9683           (gnus-deaden-summary)
9684         (gnus-close-group group)
9685         (gnus-summary-clear-local-variables)
9686         (set-buffer gnus-group-buffer)
9687         (gnus-summary-clear-local-variables)
9688         (when (get-buffer gnus-summary-buffer)
9689           (kill-buffer gnus-summary-buffer)))
9690       (when gnus-use-trees
9691         (gnus-tree-close group))
9692       (when (get-buffer gnus-article-buffer)
9693         (bury-buffer gnus-article-buffer))
9694       ;; Return to the group buffer.
9695       (gnus-configure-windows 'group 'force)
9696       ;; Clear the current group name.
9697       (setq gnus-newsgroup-name nil)
9698       (when (equal (gnus-group-group-name) group)
9699         (gnus-group-next-unread-group 1))
9700       (when quit-config
9701         (if (not (buffer-name (car quit-config)))
9702             (gnus-configure-windows 'group 'force)
9703           (set-buffer (car quit-config))
9704           (when (eq major-mode 'gnus-summary-mode)
9705             (gnus-set-global-variables))
9706           (gnus-configure-windows (cdr quit-config)))))))
9707
9708 ;;; Dead summaries.
9709
9710 (defvar gnus-dead-summary-mode-map nil)
9711
9712 (if gnus-dead-summary-mode-map
9713     nil
9714   (setq gnus-dead-summary-mode-map (make-keymap))
9715   (suppress-keymap gnus-dead-summary-mode-map)
9716   (substitute-key-definition
9717    'undefined 'gnus-summary-wake-up-the-dead gnus-dead-summary-mode-map)
9718   (let ((keys '("\C-d" "\r" "\177")))
9719     (while keys
9720       (define-key gnus-dead-summary-mode-map
9721         (pop keys) 'gnus-summary-wake-up-the-dead))))
9722
9723 (defvar gnus-dead-summary-mode nil
9724   "Minor mode for Gnus summary buffers.")
9725
9726 (defun gnus-dead-summary-mode (&optional arg)
9727   "Minor mode for Gnus summary buffers."
9728   (interactive "P")
9729   (when (eq major-mode 'gnus-summary-mode)
9730     (make-local-variable 'gnus-dead-summary-mode)
9731     (setq gnus-dead-summary-mode
9732           (if (null arg) (not gnus-dead-summary-mode)
9733             (> (prefix-numeric-value arg) 0)))
9734     (when gnus-dead-summary-mode
9735       (unless (assq 'gnus-dead-summary-mode minor-mode-alist)
9736         (push '(gnus-dead-summary-mode " Dead") minor-mode-alist))
9737       (unless (assq 'gnus-dead-summary-mode minor-mode-map-alist)
9738         (push (cons 'gnus-dead-summary-mode gnus-dead-summary-mode-map)
9739               minor-mode-map-alist)))))
9740
9741 (defun gnus-deaden-summary ()
9742   "Make the current summary buffer into a dead summary buffer."
9743   ;; Kill any previous dead summary buffer.
9744   (when (and gnus-dead-summary
9745              (buffer-name gnus-dead-summary))
9746     (save-excursion
9747       (set-buffer gnus-dead-summary)
9748       (when gnus-dead-summary-mode
9749         (kill-buffer (current-buffer)))))
9750   ;; Make this the current dead summary.
9751   (setq gnus-dead-summary (current-buffer))
9752   (gnus-dead-summary-mode 1)
9753   (let ((name (buffer-name)))
9754     (when (string-match "Summary" name)
9755       (rename-buffer
9756        (concat (substring name 0 (match-beginning 0)) "Dead "
9757                (substring name (match-beginning 0))) t))))
9758
9759 (defun gnus-kill-or-deaden-summary (buffer)
9760   "Kill or deaden the summary BUFFER."
9761   (cond (gnus-kill-summary-on-exit
9762          (when (and gnus-use-trees
9763                     (and (get-buffer buffer)
9764                          (buffer-name (get-buffer buffer))))
9765            (save-excursion
9766              (set-buffer (get-buffer buffer))
9767              (gnus-tree-close gnus-newsgroup-name)))
9768          (gnus-kill-buffer buffer))
9769         ((and (get-buffer buffer)
9770               (buffer-name (get-buffer buffer)))
9771          (save-excursion
9772            (set-buffer buffer)
9773            (gnus-deaden-summary)))))
9774
9775 (defun gnus-summary-wake-up-the-dead (&rest args)
9776   "Wake up the dead summary buffer."
9777   (interactive)
9778   (gnus-dead-summary-mode -1)
9779   (let ((name (buffer-name)))
9780     (when (string-match "Dead " name)
9781       (rename-buffer
9782        (concat (substring name 0 (match-beginning 0))
9783                (substring name (match-end 0))) t)))
9784   (gnus-message 3 "This dead summary is now alive again"))
9785
9786 ;; Suggested by Andrew Eskilsson <pi92ae@pt.hk-r.se>.
9787 (defun gnus-summary-fetch-faq (&optional faq-dir)
9788   "Fetch the FAQ for the current group.
9789 If FAQ-DIR (the prefix), prompt for a directory to search for the faq
9790 in."
9791   (interactive
9792    (list
9793     (if current-prefix-arg
9794         (completing-read
9795          "Faq dir: " (and (listp gnus-group-faq-directory)
9796                           gnus-group-faq-directory)))))
9797   (let (gnus-faq-buffer)
9798     (and (setq gnus-faq-buffer
9799                (gnus-group-fetch-faq gnus-newsgroup-name faq-dir))
9800          (gnus-configure-windows 'summary-faq))))
9801
9802 ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
9803 (defun gnus-summary-describe-group (&optional force)
9804   "Describe the current newsgroup."
9805   (interactive "P")
9806   (gnus-group-describe-group force gnus-newsgroup-name))
9807
9808 (defun gnus-summary-describe-briefly ()
9809   "Describe summary mode commands briefly."
9810   (interactive)
9811   (gnus-message 6
9812                 (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")))
9813
9814 ;; Walking around group mode buffer from summary mode.
9815
9816 (defun gnus-summary-next-group (&optional no-article target-group backward)
9817   "Exit current newsgroup and then select next unread newsgroup.
9818 If prefix argument NO-ARTICLE is non-nil, no article is selected
9819 initially.  If NEXT-GROUP, go to this group.  If BACKWARD, go to
9820 previous group instead."
9821   (interactive "P")
9822   (gnus-set-global-variables)
9823   (let ((current-group gnus-newsgroup-name)
9824         (current-buffer (current-buffer))
9825         entered)
9826     ;; First we semi-exit this group to update Xrefs and all variables.
9827     ;; We can't do a real exit, because the window conf must remain
9828     ;; the same in case the user is prompted for info, and we don't
9829     ;; want the window conf to change before that...
9830     (gnus-summary-exit t)
9831     (while (not entered)
9832       ;; Then we find what group we are supposed to enter.
9833       (set-buffer gnus-group-buffer)
9834       (gnus-group-jump-to-group current-group)
9835       (setq target-group
9836             (or target-group
9837                 (if (eq gnus-keep-same-level 'best)
9838                     (gnus-summary-best-group gnus-newsgroup-name)
9839                   (gnus-summary-search-group backward gnus-keep-same-level))))
9840       (if (not target-group)
9841           ;; There are no further groups, so we return to the group
9842           ;; buffer.
9843           (progn
9844             (gnus-message 5 "Returning to the group buffer")
9845             (setq entered t)
9846             (set-buffer current-buffer)
9847             (gnus-summary-exit))
9848         ;; We try to enter the target group.
9849         (gnus-group-jump-to-group target-group)
9850         (let ((unreads (gnus-group-group-unread)))
9851           (if (and (or (eq t unreads)
9852                        (and unreads (not (zerop unreads))))
9853                    (gnus-summary-read-group
9854                     target-group nil no-article current-buffer))
9855               (setq entered t)
9856             (setq current-group target-group
9857                   target-group nil)))))))
9858
9859 (defun gnus-summary-prev-group (&optional no-article)
9860   "Exit current newsgroup and then select previous unread newsgroup.
9861 If prefix argument NO-ARTICLE is non-nil, no article is selected initially."
9862   (interactive "P")
9863   (gnus-summary-next-group no-article nil t))
9864
9865 ;; Walking around summary lines.
9866
9867 (defun gnus-summary-first-subject (&optional unread)
9868   "Go to the first unread subject.
9869 If UNREAD is non-nil, go to the first unread article.
9870 Returns the article selected or nil if there are no unread articles."
9871   (interactive "P")
9872   (prog1
9873       (cond
9874        ;; Empty summary.
9875        ((null gnus-newsgroup-data)
9876         (gnus-message 3 "No articles in the group")
9877         nil)
9878        ;; Pick the first article.
9879        ((not unread)
9880         (goto-char (gnus-data-pos (car gnus-newsgroup-data)))
9881         (gnus-data-number (car gnus-newsgroup-data)))
9882        ;; No unread articles.
9883        ((null gnus-newsgroup-unreads)
9884         (gnus-message 3 "No more unread articles")
9885         nil)
9886        ;; Find the first unread article.
9887        (t
9888         (let ((data gnus-newsgroup-data))
9889           (while (and data
9890                       (not (gnus-data-unread-p (car data))))
9891             (setq data (cdr data)))
9892           (if data
9893               (progn
9894                 (goto-char (gnus-data-pos (car data)))
9895                 (gnus-data-number (car data)))))))
9896     (gnus-summary-position-point)))
9897
9898 (defun gnus-summary-next-subject (n &optional unread dont-display)
9899   "Go to next N'th summary line.
9900 If N is negative, go to the previous N'th subject line.
9901 If UNREAD is non-nil, only unread articles are selected.
9902 The difference between N and the actual number of steps taken is
9903 returned."
9904   (interactive "p")
9905   (let ((backward (< n 0))
9906         (n (abs n)))
9907     (while (and (> n 0)
9908                 (if backward
9909                     (gnus-summary-find-prev unread)
9910                   (gnus-summary-find-next unread)))
9911       (setq n (1- n)))
9912     (if (/= 0 n) (gnus-message 7 "No more%s articles"
9913                                (if unread " unread" "")))
9914     (or dont-display
9915         (progn
9916           (gnus-summary-recenter)
9917           (gnus-summary-position-point)))
9918     n))
9919
9920 (defun gnus-summary-next-unread-subject (n)
9921   "Go to next N'th unread summary line."
9922   (interactive "p")
9923   (gnus-summary-next-subject n t))
9924
9925 (defun gnus-summary-prev-subject (n &optional unread)
9926   "Go to previous N'th summary line.
9927 If optional argument UNREAD is non-nil, only unread article is selected."
9928   (interactive "p")
9929   (gnus-summary-next-subject (- n) unread))
9930
9931 (defun gnus-summary-prev-unread-subject (n)
9932   "Go to previous N'th unread summary line."
9933   (interactive "p")
9934   (gnus-summary-next-subject (- n) t))
9935
9936 (defun gnus-summary-goto-subject (article &optional force silent)
9937   "Go the subject line of ARTICLE.
9938 If FORCE, also allow jumping to articles not currently shown."
9939   (let ((b (point))
9940         (data (gnus-data-find article)))
9941     ;; We read in the article if we have to.
9942     (and (not data)
9943          force
9944          (gnus-summary-insert-subject article)
9945          (setq data (gnus-data-find article)))
9946     (goto-char b)
9947     (if (not data)
9948         (progn
9949           (unless silent
9950             (gnus-message 3 "Can't find article %d" article))
9951           nil)
9952       (goto-char (gnus-data-pos data))
9953       article)))
9954
9955 ;; Walking around summary lines with displaying articles.
9956
9957 (defun gnus-summary-expand-window (&optional arg)
9958   "Make the summary buffer take up the entire Emacs frame.
9959 Given a prefix, will force an `article' buffer configuration."
9960   (interactive "P")
9961   (gnus-set-global-variables)
9962   (if arg
9963       (gnus-configure-windows 'article 'force)
9964     (gnus-configure-windows 'summary 'force)))
9965
9966 (defun gnus-summary-display-article (article &optional all-header)
9967   "Display ARTICLE in article buffer."
9968   (gnus-set-global-variables)
9969   (if (null article)
9970       nil
9971     (prog1
9972         (if gnus-summary-display-article-function
9973             (funcall gnus-summary-display-article-function article all-header)
9974           (gnus-article-prepare article all-header))
9975       (run-hooks 'gnus-select-article-hook)
9976       (gnus-summary-recenter)
9977       (gnus-summary-goto-subject article)
9978       (when gnus-use-trees
9979         (gnus-possibly-generate-tree article)
9980         (gnus-highlight-selected-tree article))
9981       ;; Successfully display article.
9982       (gnus-article-set-window-start
9983        (cdr (assq article gnus-newsgroup-bookmarks)))
9984       t)))
9985
9986 (defun gnus-summary-select-article (&optional all-headers force pseudo article)
9987   "Select the current article.
9988 If ALL-HEADERS is non-nil, show all header fields.  If FORCE is
9989 non-nil, the article will be re-fetched even if it already present in
9990 the article buffer.  If PSEUDO is non-nil, pseudo-articles will also
9991 be displayed."
9992   (let ((article (or article (gnus-summary-article-number)))
9993         (all-headers (not (not all-headers))) ;Must be T or NIL.
9994         gnus-summary-display-article-function
9995         did)
9996     (and (not pseudo)
9997          (gnus-summary-article-pseudo-p article)
9998          (error "This is a pseudo-article."))
9999     (prog1
10000         (save-excursion
10001           (set-buffer gnus-summary-buffer)
10002           (if (or (and gnus-single-article-buffer
10003                        (or (null gnus-current-article)
10004                            (null gnus-article-current)
10005                            (null (get-buffer gnus-article-buffer))
10006                            (not (eq article (cdr gnus-article-current)))
10007                            (not (equal (car gnus-article-current)
10008                                        gnus-newsgroup-name))))
10009                   (and (not gnus-single-article-buffer)
10010                        (null gnus-current-article))
10011                   force)
10012               ;; The requested article is different from the current article.
10013               (prog1
10014                   (gnus-summary-display-article article all-headers)
10015                 (setq did article))
10016             (if (or all-headers gnus-show-all-headers)
10017                 (gnus-article-show-all-headers))
10018             'old))
10019       (if did
10020           (gnus-article-set-window-start
10021            (cdr (assq article gnus-newsgroup-bookmarks)))))))
10022
10023 (defun gnus-summary-set-current-mark (&optional current-mark)
10024   "Obsolete function."
10025   nil)
10026
10027 (defun gnus-summary-next-article (&optional unread subject backward push)
10028   "Select the next article.
10029 If UNREAD, only unread articles are selected.
10030 If SUBJECT, only articles with SUBJECT are selected.
10031 If BACKWARD, the previous article is selected instead of the next."
10032   (interactive "P")
10033   (gnus-set-global-variables)
10034   (let (header)
10035     (cond
10036      ;; Is there such an article?
10037      ((and (gnus-summary-search-forward unread subject backward)
10038            (or (gnus-summary-display-article (gnus-summary-article-number))
10039                (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10040       (gnus-summary-position-point))
10041      ;; If not, we try the first unread, if that is wanted.
10042      ((and subject
10043            gnus-auto-select-same
10044            (or (gnus-summary-first-unread-article)
10045                (eq (gnus-summary-article-mark) gnus-canceled-mark)))
10046       (gnus-summary-position-point)
10047       (gnus-message 6 "Wrapped"))
10048      ;; Try to get next/previous article not displayed in this group.
10049      ((and gnus-auto-extend-newsgroup
10050            (not unread) (not subject))
10051       (gnus-summary-goto-article
10052        (if backward (1- gnus-newsgroup-begin) (1+ gnus-newsgroup-end))
10053        nil t))
10054      ;; Go to next/previous group.
10055      (t
10056       (or (gnus-ephemeral-group-p gnus-newsgroup-name)
10057           (gnus-summary-jump-to-group gnus-newsgroup-name))
10058       (let ((cmd last-command-char)
10059             (group
10060              (if (eq gnus-keep-same-level 'best)
10061                  (gnus-summary-best-group gnus-newsgroup-name)
10062                (gnus-summary-search-group backward gnus-keep-same-level))))
10063         ;; For some reason, the group window gets selected.  We change
10064         ;; it back.
10065         (select-window (get-buffer-window (current-buffer)))
10066         ;; Select next unread newsgroup automagically.
10067         (cond
10068          ((not gnus-auto-select-next)
10069           (gnus-message 7 "No more%s articles" (if unread " unread" "")))
10070          ((or (eq gnus-auto-select-next 'quietly)
10071               (and (eq gnus-auto-select-next 'slightly-quietly)
10072                    push)
10073               (and (eq gnus-auto-select-next 'almost-quietly)
10074                    (gnus-summary-last-article-p)))
10075           ;; Select quietly.
10076           (if (gnus-ephemeral-group-p gnus-newsgroup-name)
10077               (gnus-summary-exit)
10078             (gnus-message 7 "No more%s articles (%s)..."
10079                           (if unread " unread" "")
10080                           (if group (concat "selecting " group)
10081                             "exiting"))
10082             (gnus-summary-next-group nil group backward)))
10083          (t
10084           (gnus-summary-walk-group-buffer
10085            gnus-newsgroup-name cmd unread backward))))))))
10086
10087 (defun gnus-summary-walk-group-buffer (from-group cmd unread backward)
10088   (let ((keystrokes '((?\C-n (gnus-group-next-unread-group 1))
10089                       (?\C-p (gnus-group-prev-unread-group 1))))
10090         keve key group ended)
10091     (save-excursion
10092       (set-buffer gnus-group-buffer)
10093       (gnus-summary-jump-to-group from-group)
10094       (setq group
10095             (if (eq gnus-keep-same-level 'best)
10096                 (gnus-summary-best-group gnus-newsgroup-name)
10097               (gnus-summary-search-group backward gnus-keep-same-level))))
10098     (while (not ended)
10099       (gnus-message
10100        5 "No more%s articles%s" (if unread " unread" "")
10101        (if (and group
10102                 (not (gnus-ephemeral-group-p gnus-newsgroup-name)))
10103            (format " (Type %s for %s [%s])"
10104                    (single-key-description cmd) group
10105                    (car (gnus-gethash group gnus-newsrc-hashtb)))
10106          (format " (Type %s to exit %s)"
10107                  (single-key-description cmd)
10108                  gnus-newsgroup-name)))
10109       ;; Confirm auto selection.
10110       (setq key (car (setq keve (gnus-read-event-char))))
10111       (setq ended t)
10112       (cond
10113        ((assq key keystrokes)
10114         (let ((obuf (current-buffer)))
10115           (switch-to-buffer gnus-group-buffer)
10116           (and group
10117                (gnus-group-jump-to-group group))
10118           (eval (cadr (assq key keystrokes)))
10119           (setq group (gnus-group-group-name))
10120           (switch-to-buffer obuf))
10121         (setq ended nil))
10122        ((equal key cmd)
10123         (if (or (not group)
10124                 (gnus-ephemeral-group-p gnus-newsgroup-name))
10125             (gnus-summary-exit)
10126           (gnus-summary-next-group nil group backward)))
10127        (t
10128         (push (cdr keve) unread-command-events))))))
10129
10130 (defun gnus-read-event-char ()
10131   "Get the next event."
10132   (let ((event (read-event)))
10133     (cons (and (numberp event) event) event)))
10134
10135 (defun gnus-summary-next-unread-article ()
10136   "Select unread article after current one."
10137   (interactive)
10138   (gnus-summary-next-article t (and gnus-auto-select-same
10139                                     (gnus-summary-article-subject))))
10140
10141 (defun gnus-summary-prev-article (&optional unread subject)
10142   "Select the article after the current one.
10143 If UNREAD is non-nil, only unread articles are selected."
10144   (interactive "P")
10145   (gnus-summary-next-article unread subject t))
10146
10147 (defun gnus-summary-prev-unread-article ()
10148   "Select unred article before current one."
10149   (interactive)
10150   (gnus-summary-prev-article t (and gnus-auto-select-same
10151                                     (gnus-summary-article-subject))))
10152
10153 (defun gnus-summary-next-page (&optional lines circular)
10154   "Show next page of the selected article.
10155 If at the end of the current article, select the next article.
10156 LINES says how many lines should be scrolled up.
10157
10158 If CIRCULAR is non-nil, go to the start of the article instead of
10159 selecting the next article when reaching the end of the current
10160 article."
10161   (interactive "P")
10162   (setq gnus-summary-buffer (current-buffer))
10163   (gnus-set-global-variables)
10164   (let ((article (gnus-summary-article-number))
10165         (endp nil))
10166     (gnus-configure-windows 'article)
10167     (if (or (null gnus-current-article)
10168             (null gnus-article-current)
10169             (/= article (cdr gnus-article-current))
10170             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10171         ;; Selected subject is different from current article's.
10172         (gnus-summary-display-article article)
10173       (gnus-eval-in-buffer-window
10174        gnus-article-buffer
10175        (setq endp (gnus-article-next-page lines)))
10176       (if endp
10177           (cond (circular
10178                  (gnus-summary-beginning-of-article))
10179                 (lines
10180                  (gnus-message 3 "End of message"))
10181                 ((null lines)
10182                  (if (and (eq gnus-summary-goto-unread 'never)
10183                           (not (eq article gnus-newsgroup-end)))
10184                      (gnus-summary-next-article)
10185                    (gnus-summary-next-unread-article))))))
10186     (gnus-summary-recenter)
10187     (gnus-summary-position-point)))
10188
10189 (defun gnus-summary-prev-page (&optional lines)
10190   "Show previous page of selected article.
10191 Argument LINES specifies lines to be scrolled down."
10192   (interactive "P")
10193   (gnus-set-global-variables)
10194   (let ((article (gnus-summary-article-number)))
10195     (gnus-configure-windows 'article)
10196     (if (or (null gnus-current-article)
10197             (null gnus-article-current)
10198             (/= article (cdr gnus-article-current))
10199             (not (equal (car gnus-article-current) gnus-newsgroup-name)))
10200         ;; Selected subject is different from current article's.
10201         (gnus-summary-display-article article)
10202       (gnus-summary-recenter)
10203       (gnus-eval-in-buffer-window gnus-article-buffer
10204                                   (gnus-article-prev-page lines))))
10205   (gnus-summary-position-point))
10206
10207 (defun gnus-summary-scroll-up (lines)
10208   "Scroll up (or down) one line current article.
10209 Argument LINES specifies lines to be scrolled up (or down if negative)."
10210   (interactive "p")
10211   (gnus-set-global-variables)
10212   (gnus-configure-windows 'article)
10213   (when (eq (gnus-summary-select-article nil nil 'pseudo) 'old)
10214     (gnus-eval-in-buffer-window
10215      gnus-article-buffer
10216      (cond ((> lines 0)
10217             (if (gnus-article-next-page lines)
10218                 (gnus-message 3 "End of message")))
10219            ((< lines 0)
10220             (gnus-article-prev-page (- lines))))))
10221   (gnus-summary-recenter)
10222   (gnus-summary-position-point))
10223
10224 (defun gnus-summary-next-same-subject ()
10225   "Select next article which has the same subject as current one."
10226   (interactive)
10227   (gnus-set-global-variables)
10228   (gnus-summary-next-article nil (gnus-summary-article-subject)))
10229
10230 (defun gnus-summary-prev-same-subject ()
10231   "Select previous article which has the same subject as current one."
10232   (interactive)
10233   (gnus-set-global-variables)
10234   (gnus-summary-prev-article nil (gnus-summary-article-subject)))
10235
10236 (defun gnus-summary-next-unread-same-subject ()
10237   "Select next unread article which has the same subject as current one."
10238   (interactive)
10239   (gnus-set-global-variables)
10240   (gnus-summary-next-article t (gnus-summary-article-subject)))
10241
10242 (defun gnus-summary-prev-unread-same-subject ()
10243   "Select previous unread article which has the same subject as current one."
10244   (interactive)
10245   (gnus-set-global-variables)
10246   (gnus-summary-prev-article t (gnus-summary-article-subject)))
10247
10248 (defun gnus-summary-first-unread-article ()
10249   "Select the first unread article.
10250 Return nil if there are no unread articles."
10251   (interactive)
10252   (gnus-set-global-variables)
10253   (prog1
10254       (if (gnus-summary-first-subject t)
10255           (progn
10256             (gnus-summary-show-thread)
10257             (gnus-summary-first-subject t)
10258             (gnus-summary-display-article (gnus-summary-article-number))))
10259     (gnus-summary-position-point)))
10260
10261 (defun gnus-summary-best-unread-article ()
10262   "Select the unread article with the highest score."
10263   (interactive)
10264   (gnus-set-global-variables)
10265   (let ((best -1000000)
10266         (data gnus-newsgroup-data)
10267         article score)
10268     (while data
10269       (and (gnus-data-unread-p (car data))
10270            (> (setq score
10271                     (gnus-summary-article-score (gnus-data-number (car data))))
10272               best)
10273            (setq best score
10274                  article (gnus-data-number (car data))))
10275       (setq data (cdr data)))
10276     (if article
10277         (gnus-summary-goto-article article)
10278       (error "No unread articles"))
10279     (gnus-summary-position-point)))
10280
10281 (defun gnus-summary-last-subject ()
10282   "Go to the last displayed subject line in the group."
10283   (let ((article (gnus-data-number (car (gnus-data-list t)))))
10284     (when article
10285       (gnus-summary-goto-subject article))))
10286
10287 (defun gnus-summary-goto-article (article &optional all-headers force)
10288   "Fetch ARTICLE and display it if it exists.
10289 If ALL-HEADERS is non-nil, no header lines are hidden."
10290   (interactive
10291    (list
10292     (string-to-int
10293      (completing-read
10294       "Article number: "
10295       (mapcar (lambda (number) (list (int-to-string number)))
10296               gnus-newsgroup-limit)))
10297     current-prefix-arg
10298     t))
10299   (prog1
10300       (if (gnus-summary-goto-subject article force)
10301           (gnus-summary-display-article article all-headers)
10302         (gnus-message 4 "Couldn't go to article %s" article) nil)
10303     (gnus-summary-position-point)))
10304
10305 (defun gnus-summary-goto-last-article ()
10306   "Go to the previously read article."
10307   (interactive)
10308   (prog1
10309       (and gnus-last-article
10310            (gnus-summary-goto-article gnus-last-article))
10311     (gnus-summary-position-point)))
10312
10313 (defun gnus-summary-pop-article (number)
10314   "Pop one article off the history and go to the previous.
10315 NUMBER articles will be popped off."
10316   (interactive "p")
10317   (let (to)
10318     (setq gnus-newsgroup-history
10319           (cdr (setq to (nthcdr number gnus-newsgroup-history))))
10320     (if to
10321         (gnus-summary-goto-article (car to))
10322       (error "Article history empty")))
10323   (gnus-summary-position-point))
10324
10325 ;; Summary commands and functions for limiting the summary buffer.
10326
10327 (defun gnus-summary-limit-to-articles (n)
10328   "Limit the summary buffer to the next N articles.
10329 If not given a prefix, use the process marked articles instead."
10330   (interactive "P")
10331   (gnus-set-global-variables)
10332   (prog1
10333       (let ((articles (gnus-summary-work-articles n)))
10334         (setq gnus-newsgroup-processable nil)
10335         (gnus-summary-limit articles))
10336     (gnus-summary-position-point)))
10337
10338 (defun gnus-summary-pop-limit (&optional total)
10339   "Restore the previous limit.
10340 If given a prefix, remove all limits."
10341   (interactive "P")
10342   (gnus-set-global-variables)
10343   (when total 
10344     (setq gnus-newsgroup-limits
10345           (list (mapcar (lambda (h) (mail-header-number h))
10346                         gnus-newsgroup-headers))))
10347   (unless gnus-newsgroup-limits
10348     (error "No limit to pop"))
10349   (prog1
10350       (gnus-summary-limit nil 'pop)
10351     (gnus-summary-position-point)))
10352
10353 (defun gnus-summary-limit-to-subject (subject &optional header)
10354   "Limit the summary buffer to articles that have subjects that match a regexp."
10355   (interactive "sRegexp: ")
10356   (unless header
10357     (setq header "subject"))
10358   (when (not (equal "" subject))
10359     (prog1
10360         (let ((articles (gnus-summary-find-matching
10361                          (or header "subject") subject 'all)))
10362           (or articles (error "Found no matches for \"%s\"" subject))
10363           (gnus-summary-limit articles))
10364       (gnus-summary-position-point))))
10365
10366 (defun gnus-summary-limit-to-author (from)
10367   "Limit the summary buffer to articles that have authors that match a regexp."
10368   (interactive "sRegexp: ")
10369   (gnus-summary-limit-to-subject from "from"))
10370
10371 (defalias 'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10372 (make-obsolete
10373  'gnus-summary-delete-marked-as-read 'gnus-summary-limit-to-unread)
10374
10375 (defun gnus-summary-limit-to-unread (&optional all)
10376   "Limit the summary buffer to articles that are not marked as read.
10377 If ALL is non-nil, limit strictly to unread articles."
10378   (interactive "P")
10379   (if all
10380       (gnus-summary-limit-to-marks (char-to-string gnus-unread-mark))
10381     (gnus-summary-limit-to-marks
10382      ;; Concat all the marks that say that an article is read and have
10383      ;; those removed.
10384      (list gnus-del-mark gnus-read-mark gnus-ancient-mark
10385            gnus-killed-mark gnus-kill-file-mark
10386            gnus-low-score-mark gnus-expirable-mark
10387            gnus-canceled-mark gnus-catchup-mark gnus-sparse-mark)
10388      'reverse)))
10389
10390 (defalias 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10391 (make-obsolete 'gnus-summary-delete-marked-with 'gnus-summary-limit-to-marks)
10392
10393 (defun gnus-summary-limit-to-marks (marks &optional reverse)
10394   "Limit the summary buffer to articles that are marked with MARKS (e.g. \"DK\").
10395 If REVERSE, limit the summary buffer to articles that are not marked
10396 with MARKS.  MARKS can either be a string of marks or a list of marks.
10397 Returns how many articles were removed."
10398   (interactive "sMarks: ")
10399   (gnus-set-global-variables)
10400   (prog1
10401       (let ((data gnus-newsgroup-data)
10402             (marks (if (listp marks) marks
10403                      (append marks nil))) ; Transform to list.
10404             articles)
10405         (while data
10406           (and (if reverse (not (memq (gnus-data-mark (car data)) marks))
10407                  (memq (gnus-data-mark (car data)) marks))
10408                (setq articles (cons (gnus-data-number (car data)) articles)))
10409           (setq data (cdr data)))
10410         (gnus-summary-limit articles))
10411     (gnus-summary-position-point)))
10412
10413 (defun gnus-summary-limit-to-score (&optional score)
10414   "Limit to articles with score at or above SCORE."
10415   (interactive "P")
10416   (gnus-set-global-variables)
10417   (setq score (if score
10418                   (prefix-numeric-value score)
10419                 (or gnus-summary-default-score 0)))
10420   (let ((data gnus-newsgroup-data)
10421         articles)
10422     (while data
10423       (when (>= (gnus-summary-article-score (gnus-data-number (car data)))
10424                 score)
10425         (push (gnus-data-number (car data)) articles))
10426       (setq data (cdr data)))
10427     (prog1
10428         (gnus-summary-limit articles)
10429       (gnus-summary-position-point))))
10430
10431 (defun gnus-summary-limit-include-dormant ()
10432   "Display all the hidden articles that are marked as dormant."
10433   (interactive)
10434   (gnus-set-global-variables)
10435   (or gnus-newsgroup-dormant
10436       (error "There are no dormant articles in this group"))
10437   (prog1
10438       (gnus-summary-limit (append gnus-newsgroup-dormant gnus-newsgroup-limit))
10439     (gnus-summary-position-point)))
10440
10441 (defun gnus-summary-limit-exclude-dormant ()
10442   "Hide all dormant articles."
10443   (interactive)
10444   (gnus-set-global-variables)
10445   (prog1
10446       (gnus-summary-limit-to-marks (list gnus-dormant-mark) 'reverse)
10447     (gnus-summary-position-point)))
10448
10449 (defun gnus-summary-limit-exclude-childless-dormant ()
10450   "Hide all dormant articles that have no children."
10451   (interactive)
10452   (gnus-set-global-variables)
10453   (let ((data (gnus-data-list t))
10454         articles d children)
10455     ;; Find all articles that are either not dormant or have
10456     ;; children.
10457     (while (setq d (pop data))
10458       (when (or (not (= (gnus-data-mark d) gnus-dormant-mark))
10459                 (and (setq children 
10460                            (gnus-article-children (gnus-data-number d)))
10461                      (let (found)
10462                        (while children
10463                          (when (memq (car children) articles)
10464                            (setq children nil
10465                                  found t))
10466                          (pop children))
10467                        found)))
10468         (push (gnus-data-number d) articles)))
10469     ;; Do the limiting.
10470     (prog1
10471         (gnus-summary-limit articles)
10472       (gnus-summary-position-point))))
10473
10474 (defun gnus-summary-limit-mark-excluded-as-read (&optional all)
10475   "Mark all unread excluded articles as read.
10476 If ALL, mark even excluded ticked and dormants as read."
10477   (interactive "P")
10478   (let ((articles (gnus-sorted-complement
10479                    (sort
10480                     (mapcar (lambda (h) (mail-header-number h))
10481                             gnus-newsgroup-headers)
10482                     '<)
10483                    (sort gnus-newsgroup-limit '<)))
10484         article)
10485     (setq gnus-newsgroup-unreads nil)
10486     (if all
10487         (setq gnus-newsgroup-dormant nil
10488               gnus-newsgroup-marked nil
10489               gnus-newsgroup-reads
10490               (nconc
10491                (mapcar (lambda (n) (cons n gnus-catchup-mark)) articles)
10492                gnus-newsgroup-reads))
10493       (while (setq article (pop articles))
10494         (unless (or (memq article gnus-newsgroup-dormant)
10495                     (memq article gnus-newsgroup-marked))
10496           (push (cons article gnus-catchup-mark) gnus-newsgroup-reads))))))
10497
10498 (defun gnus-summary-limit (articles &optional pop)
10499   (if pop
10500       ;; We pop the previous limit off the stack and use that.
10501       (setq articles (car gnus-newsgroup-limits)
10502             gnus-newsgroup-limits (cdr gnus-newsgroup-limits))
10503     ;; We use the new limit, so we push the old limit on the stack.
10504     (setq gnus-newsgroup-limits
10505           (cons gnus-newsgroup-limit gnus-newsgroup-limits)))
10506   ;; Set the limit.
10507   (setq gnus-newsgroup-limit articles)
10508   (let ((total (length gnus-newsgroup-data))
10509         (data (gnus-data-find-list (gnus-summary-article-number)))
10510         found)
10511     ;; This will do all the work of generating the new summary buffer
10512     ;; according to the new limit.
10513     (gnus-summary-prepare)
10514     ;; Hide any threads, possibly.
10515     (and gnus-show-threads
10516          gnus-thread-hide-subtree
10517          (gnus-summary-hide-all-threads))
10518     ;; Try to return to the article you were at, or one in the
10519     ;; neighborhood.
10520     (if data
10521         ;; We try to find some article after the current one.
10522         (while data
10523           (and (gnus-summary-goto-subject
10524                 (gnus-data-number (car data)) nil t)
10525                (setq data nil
10526                      found t))
10527           (setq data (cdr data))))
10528     (or found
10529         ;; If there is no data, that means that we were after the last
10530         ;; article.  The same goes when we can't find any articles
10531         ;; after the current one.
10532         (progn
10533           (goto-char (point-max))
10534           (gnus-summary-find-prev)))
10535     ;; We return how many articles were removed from the summary
10536     ;; buffer as a result of the new limit.
10537     (- total (length gnus-newsgroup-data))))
10538
10539 (defsubst gnus-cut-thread (thread)
10540   "Go forwards in the thread until we find an article that we want to display."
10541   (if (eq gnus-fetch-old-headers 'some)
10542       (while (and thread
10543                   (memq (mail-header-number (car thread)) 
10544                         gnus-newsgroup-ancient)
10545                   (<= (length (cdr thread)) 1))
10546         (setq thread (cadr thread)))
10547     (while (and thread
10548                 (memq (mail-header-number (car thread)) gnus-newsgroup-sparse)
10549                 (= (length (cdr thread)) 1))
10550       (setq thread (cadr thread))))
10551   thread)
10552
10553 (defun gnus-cut-threads (threads)
10554   "Cut off all uninteresting articles from the beginning of threads."
10555   (when (or (eq gnus-fetch-old-headers 'some)
10556             (eq gnus-build-sparse-threads 'some)
10557             (eq gnus-build-sparse-threads 'more))
10558     (let ((th threads))
10559       (while th
10560         (setcar th (gnus-cut-thread (car th)))
10561         (setq th (cdr th)))))
10562   threads)
10563
10564 (defun gnus-summary-initial-limit (&optional show-if-empty)
10565   "Figure out what the initial limit is supposed to be on group entry.
10566 This entails weeding out unwanted dormants, low-scored articles,
10567 fetch-old-headers verbiage, and so on."
10568   ;; Most groups have nothing to remove.
10569   (if (or gnus-inhibit-limiting
10570           (and (null gnus-newsgroup-dormant)
10571                (not (eq gnus-fetch-old-headers 'some))
10572                (null gnus-summary-expunge-below)
10573                (not (eq gnus-build-sparse-threads 'some))
10574                (not (eq gnus-build-sparse-threads 'more))
10575                (null gnus-thread-expunge-below)
10576                (not gnus-use-nocem)))
10577       () ; Do nothing.
10578     (push gnus-newsgroup-limit gnus-newsgroup-limits)
10579     (setq gnus-newsgroup-limit nil)
10580     (mapatoms
10581      (lambda (node)
10582        (unless (car (symbol-value node))
10583          ;; These threads have no parents -- they are roots.
10584          (let ((nodes (cdr (symbol-value node)))
10585                thread)
10586            (while nodes
10587              (if (and gnus-thread-expunge-below
10588                       (< (gnus-thread-total-score (car nodes))
10589                          gnus-thread-expunge-below))
10590                  (gnus-expunge-thread (pop nodes))
10591                (setq thread (pop nodes))
10592                (gnus-summary-limit-children thread))))))
10593      gnus-newsgroup-dependencies)
10594     ;; If this limitation resulted in an empty group, we might
10595     ;; pop the previous limit and use it instead.
10596     (when (and (not gnus-newsgroup-limit)
10597                show-if-empty)
10598       (setq gnus-newsgroup-limit (pop gnus-newsgroup-limits)))
10599     gnus-newsgroup-limit))
10600
10601 (defun gnus-summary-limit-children (thread)
10602   "Return 1 if this subthread is visible and 0 if it is not."
10603   ;; First we get the number of visible children to this thread.  This
10604   ;; is done by recursing down the thread using this function, so this
10605   ;; will really go down to a leaf article first, before slowly
10606   ;; working its way up towards the root.
10607   (when thread
10608     (let ((children
10609            (if (cdr thread)
10610                (apply '+ (mapcar 'gnus-summary-limit-children
10611                                  (cdr thread)))
10612              0))
10613           (number (mail-header-number (car thread)))
10614           score)
10615       (if (or
10616            ;; If this article is dormant and has absolutely no visible
10617            ;; children, then this article isn't visible.
10618            (and (memq number gnus-newsgroup-dormant)
10619                 (= children 0))
10620            ;; If this is a "fetch-old-headered" and there is only one
10621            ;; visible child (or less), then we don't want this article.
10622            (and (eq gnus-fetch-old-headers 'some)
10623                 (memq number gnus-newsgroup-ancient)
10624                 (zerop children))
10625            ;; If this is a sparsely inserted article with no children,
10626            ;; we don't want it.
10627            (and (eq gnus-build-sparse-threads 'some)
10628                 (memq number gnus-newsgroup-sparse)
10629                 (zerop children))
10630            ;; If we use expunging, and this article is really
10631            ;; low-scored, then we don't want this article.
10632            (when (and gnus-summary-expunge-below
10633                       (< (setq score
10634                                (or (cdr (assq number gnus-newsgroup-scored))
10635                                    gnus-summary-default-score))
10636                          gnus-summary-expunge-below))
10637              ;; We increase the expunge-tally here, but that has
10638              ;; nothing to do with the limits, really.
10639              (incf gnus-newsgroup-expunged-tally)
10640              ;; We also mark as read here, if that's wanted.
10641              (when (and gnus-summary-mark-below
10642                         (< score gnus-summary-mark-below))
10643                (setq gnus-newsgroup-unreads
10644                      (delq number gnus-newsgroup-unreads))
10645                (if gnus-newsgroup-auto-expire
10646                    (push number gnus-newsgroup-expirable)
10647                  (push (cons number gnus-low-score-mark)
10648                        gnus-newsgroup-reads)))
10649              t)
10650            (and gnus-use-nocem
10651                 (gnus-nocem-unwanted-article-p (mail-header-id (car thread)))))
10652           ;; Nope, invisible article.
10653           0
10654         ;; Ok, this article is to be visible, so we add it to the limit
10655         ;; and return 1.
10656         (setq gnus-newsgroup-limit (cons number gnus-newsgroup-limit))
10657         1))))
10658
10659 (defun gnus-expunge-thread (thread)
10660   "Mark all articles in THREAD as read."
10661   (let* ((number (mail-header-number (car thread))))
10662     (incf gnus-newsgroup-expunged-tally)
10663     ;; We also mark as read here, if that's wanted.
10664     (setq gnus-newsgroup-unreads
10665           (delq number gnus-newsgroup-unreads))
10666     (if gnus-newsgroup-auto-expire
10667         (push number gnus-newsgroup-expirable)
10668       (push (cons number gnus-low-score-mark)
10669             gnus-newsgroup-reads)))
10670   ;; Go recursively through all subthreads.
10671   (mapcar 'gnus-expunge-thread (cdr thread)))
10672
10673 ;; Summary article oriented commands
10674
10675 (defun gnus-summary-refer-parent-article (n)
10676   "Refer parent article N times.
10677 The difference between N and the number of articles fetched is returned."
10678   (interactive "p")
10679   (gnus-set-global-variables)
10680   (while
10681       (and
10682        (> n 0)
10683        (let* ((header (gnus-summary-article-header))
10684               (ref
10685                ;; If we try to find the parent of the currently
10686                ;; displayed article, then we take a look at the actual
10687                ;; References header, since this is slightly more
10688                ;; reliable than the References field we got from the
10689                ;; server.
10690                (if (and (eq (mail-header-number header)
10691                             (cdr gnus-article-current))
10692                         (equal gnus-newsgroup-name
10693                                (car gnus-article-current)))
10694                    (save-excursion
10695                      (set-buffer gnus-original-article-buffer)
10696                      (nnheader-narrow-to-headers)
10697                      (prog1
10698                          (mail-fetch-field "references")
10699                        (widen)))
10700                  ;; It's not the current article, so we take a bet on
10701                  ;; the value we got from the server.
10702                  (mail-header-references header))))
10703          (if (setq ref (or ref (mail-header-references header)))
10704              (or (gnus-summary-refer-article (gnus-parent-id ref))
10705                  (gnus-message 1 "Couldn't find parent"))
10706            (gnus-message 1 "No references in article %d"
10707                          (gnus-summary-article-number))
10708            nil)))
10709     (setq n (1- n)))
10710   (gnus-summary-position-point)
10711   n)
10712
10713 (defun gnus-summary-refer-references ()
10714   "Fetch all articles mentioned in the References header.
10715 Return how many articles were fetched."
10716   (interactive)
10717   (gnus-set-global-variables)
10718   (let ((ref (mail-header-references (gnus-summary-article-header)))
10719         (current (gnus-summary-article-number))
10720         (n 0))
10721     ;; For each Message-ID in the References header...
10722     (while (string-match "<[^>]*>" ref)
10723       (incf n)
10724       ;; ... fetch that article.
10725       (gnus-summary-refer-article
10726        (prog1 (match-string 0 ref)
10727          (setq ref (substring ref (match-end 0))))))
10728     (gnus-summary-goto-subject current)
10729     (gnus-summary-position-point)
10730     n))
10731
10732 (defun gnus-summary-refer-article (message-id)
10733   "Fetch an article specified by MESSAGE-ID."
10734   (interactive "sMessage-ID: ")
10735   (when (and (stringp message-id)
10736              (not (zerop (length message-id))))
10737     ;; Construct the correct Message-ID if necessary.
10738     ;; Suggested by tale@pawl.rpi.edu.
10739     (unless (string-match "^<" message-id)
10740       (setq message-id (concat "<" message-id)))
10741     (unless (string-match ">$" message-id)
10742       (setq message-id (concat message-id ">")))
10743     (let ((header (car (gnus-gethash (downcase message-id)
10744                                      gnus-newsgroup-dependencies))))
10745       (if header
10746           ;; The article is present in the buffer, to we just go to it.
10747           (gnus-summary-goto-article (mail-header-number header) nil t)
10748         ;; We fetch the article
10749         (let ((gnus-override-method gnus-refer-article-method)
10750               number)
10751           ;; Start the special refer-article method, if necessary.
10752           (when gnus-refer-article-method
10753             (gnus-check-server gnus-refer-article-method))
10754           ;; Fetch the header, and display the article.
10755           (if (setq number (gnus-summary-insert-subject message-id))
10756               (gnus-summary-select-article nil nil nil number)
10757             (gnus-message 3 "Couldn't fetch article %s" message-id)))))))
10758
10759 (defun gnus-summary-enter-digest-group (&optional force)
10760   "Enter a digest group based on the current article."
10761   (interactive "P")
10762   (gnus-set-global-variables)
10763   (gnus-summary-select-article)
10764   (let ((name (format "%s-%d"
10765                       (gnus-group-prefixed-name
10766                        gnus-newsgroup-name (list 'nndoc ""))
10767                       gnus-current-article))
10768         (ogroup gnus-newsgroup-name)
10769         (case-fold-search t)
10770         (buf (current-buffer))
10771         dig)
10772     (save-excursion
10773       (setq dig (nnheader-set-temp-buffer " *gnus digest buffer*"))
10774       (insert-buffer-substring gnus-original-article-buffer)
10775       (narrow-to-region
10776        (goto-char (point-min))
10777        (or (search-forward "\n\n" nil t) (point)))
10778       (goto-char (point-min))
10779       (delete-matching-lines "^\\(Path\\):\\|^From ")
10780       (widen))
10781     (unwind-protect
10782         (if (gnus-group-read-ephemeral-group
10783              name `(nndoc ,name (nndoc-address
10784                                  ,(get-buffer dig))
10785                           (nndoc-article-type ,(if force 'digest 'guess))) t)
10786             ;; Make all postings to this group go to the parent group.
10787             (nconc (gnus-info-params (gnus-get-info name))
10788                    (list (cons 'to-group ogroup)))
10789           ;; Couldn't select this doc group.
10790           (switch-to-buffer buf)
10791           (gnus-set-global-variables)
10792           (gnus-configure-windows 'summary)
10793           (gnus-message 3 "Article couldn't be entered?"))
10794       (kill-buffer dig))))
10795
10796 (defun gnus-summary-isearch-article (&optional regexp-p)
10797   "Do incremental search forward on the current article.
10798 If REGEXP-P (the prefix) is non-nil, do regexp isearch."
10799   (interactive "P")
10800   (gnus-set-global-variables)
10801   (gnus-summary-select-article)
10802   (gnus-configure-windows 'article)
10803   (gnus-eval-in-buffer-window
10804    gnus-article-buffer
10805    (goto-char (point-min))
10806    (isearch-forward regexp-p)))
10807
10808 (defun gnus-summary-search-article-forward (regexp &optional backward)
10809   "Search for an article containing REGEXP forward.
10810 If BACKWARD, search backward instead."
10811   (interactive
10812    (list (read-string
10813           (format "Search article %s (regexp%s): "
10814                   (if current-prefix-arg "backward" "forward")
10815                   (if gnus-last-search-regexp
10816                       (concat ", default " gnus-last-search-regexp)
10817                     "")))
10818          current-prefix-arg))
10819   (gnus-set-global-variables)
10820   (if (string-equal regexp "")
10821       (setq regexp (or gnus-last-search-regexp ""))
10822     (setq gnus-last-search-regexp regexp))
10823   (if (gnus-summary-search-article regexp backward)
10824       (gnus-article-set-window-start
10825        (cdr (assq (gnus-summary-article-number) gnus-newsgroup-bookmarks)))
10826     (error "Search failed: \"%s\"" regexp)))
10827
10828 (defun gnus-summary-search-article-backward (regexp)
10829   "Search for an article containing REGEXP backward."
10830   (interactive
10831    (list (read-string
10832           (format "Search article backward (regexp%s): "
10833                   (if gnus-last-search-regexp
10834                       (concat ", default " gnus-last-search-regexp)
10835                     "")))))
10836   (gnus-summary-search-article-forward regexp 'backward))
10837
10838 (defun gnus-summary-search-article (regexp &optional backward)
10839   "Search for an article containing REGEXP.
10840 Optional argument BACKWARD means do search for backward.
10841 gnus-select-article-hook is not called during the search."
10842   (let ((gnus-select-article-hook nil)  ;Disable hook.
10843         (gnus-mark-article-hook nil)    ;Inhibit marking as read.
10844         (re-search
10845          (if backward
10846              (function re-search-backward) (function re-search-forward)))
10847         (found nil)
10848         (last nil))
10849     ;; Hidden thread subtrees must be searched for ,too.
10850     (gnus-summary-show-all-threads)
10851     ;; First of all, search current article.
10852     ;; We don't want to read article again from NNTP server nor reset
10853     ;; current point.
10854     (gnus-summary-select-article)
10855     (gnus-message 9 "Searching article: %d..." gnus-current-article)
10856     (setq last gnus-current-article)
10857     (gnus-eval-in-buffer-window
10858      gnus-article-buffer
10859      (save-restriction
10860        (widen)
10861        ;; Begin search from current point.
10862        (setq found (funcall re-search regexp nil t))))
10863     ;; Then search next articles.
10864     (while (and (not found)
10865                 (gnus-summary-display-article
10866                  (if backward (gnus-summary-find-prev)
10867                    (gnus-summary-find-next))))
10868       (gnus-message 9 "Searching article: %d..." gnus-current-article)
10869       (gnus-eval-in-buffer-window
10870        gnus-article-buffer
10871        (save-restriction
10872          (widen)
10873          (goto-char (if backward (point-max) (point-min)))
10874          (setq found (funcall re-search regexp nil t)))))
10875     (message "")
10876     ;; Adjust article pointer.
10877     (or (eq last gnus-current-article)
10878         (setq gnus-last-article last))
10879     ;; Return T if found such article.
10880     found))
10881
10882 (defun gnus-summary-find-matching (header regexp &optional backward unread
10883                                           not-case-fold)
10884   "Return a list of all articles that match REGEXP on HEADER.
10885 The search stars on the current article and goes forwards unless
10886 BACKWARD is non-nil.  If BACKWARD is `all', do all articles.
10887 If UNREAD is non-nil, only unread articles will
10888 be taken into consideration.  If NOT-CASE-FOLD, case won't be folded
10889 in the comparisons."
10890   (let ((data (if (eq backward 'all) gnus-newsgroup-data
10891                 (gnus-data-find-list
10892                  (gnus-summary-article-number) (gnus-data-list backward))))
10893         (func `(lambda (h) (,(intern (concat "mail-header-" header)) h)))
10894         (case-fold-search (not not-case-fold))
10895         articles d)
10896     (or (fboundp (intern (concat "mail-header-" header)))
10897         (error "%s is not a valid header" header))
10898     (while data
10899       (setq d (car data))
10900       (and (or (not unread)             ; We want all articles...
10901                (gnus-data-unread-p d))  ; Or just unreads.
10902            (vectorp (gnus-data-header d)) ; It's not a pseudo.
10903            (string-match regexp (funcall func (gnus-data-header d))) ; Match.
10904            (setq articles (cons (gnus-data-number d) articles))) ; Success!
10905       (setq data (cdr data)))
10906     (nreverse articles)))
10907
10908 (defun gnus-summary-execute-command (header regexp command &optional backward)
10909   "Search forward for an article whose HEADER matches REGEXP and execute COMMAND.
10910 If HEADER is an empty string (or nil), the match is done on the entire
10911 article.  If BACKWARD (the prefix) is non-nil, search backward instead."
10912   (interactive
10913    (list (let ((completion-ignore-case t))
10914            (completing-read
10915             "Header name: "
10916             (mapcar (lambda (string) (list string))
10917                     '("Number" "Subject" "From" "Lines" "Date"
10918                       "Message-ID" "Xref" "References" "Body"))
10919             nil 'require-match))
10920          (read-string "Regexp: ")
10921          (read-key-sequence "Command: ")
10922          current-prefix-arg))
10923   (when (equal header "Body")
10924     (setq header ""))
10925   (gnus-set-global-variables)
10926   ;; Hidden thread subtrees must be searched as well.
10927   (gnus-summary-show-all-threads)
10928   ;; We don't want to change current point nor window configuration.
10929   (save-excursion
10930     (save-window-excursion
10931       (gnus-message 6 "Executing %s..." (key-description command))
10932       ;; We'd like to execute COMMAND interactively so as to give arguments.
10933       (gnus-execute header regexp
10934                     `(lambda () (call-interactively ',(key-binding command)))
10935                     backward)
10936       (gnus-message 6 "Executing %s...done" (key-description command)))))
10937
10938 (defun gnus-summary-beginning-of-article ()
10939   "Scroll the article back to the beginning."
10940   (interactive)
10941   (gnus-set-global-variables)
10942   (gnus-summary-select-article)
10943   (gnus-configure-windows 'article)
10944   (gnus-eval-in-buffer-window
10945    gnus-article-buffer
10946    (widen)
10947    (goto-char (point-min))
10948    (and gnus-break-pages (gnus-narrow-to-page))))
10949
10950 (defun gnus-summary-end-of-article ()
10951   "Scroll to the end of the article."
10952   (interactive)
10953   (gnus-set-global-variables)
10954   (gnus-summary-select-article)
10955   (gnus-configure-windows 'article)
10956   (gnus-eval-in-buffer-window
10957    gnus-article-buffer
10958    (widen)
10959    (goto-char (point-max))
10960    (recenter -3)
10961    (and gnus-break-pages (gnus-narrow-to-page))))
10962
10963 (defun gnus-summary-show-article (&optional arg)
10964   "Force re-fetching of the current article.
10965 If ARG (the prefix) is non-nil, show the raw article without any
10966 article massaging functions being run."
10967   (interactive "P")
10968   (gnus-set-global-variables)
10969   (if (not arg)
10970       ;; Select the article the normal way.
10971       (gnus-summary-select-article nil 'force)
10972     ;; Bind the article treatment functions to nil.
10973     (let ((gnus-have-all-headers t)
10974           gnus-article-display-hook
10975           gnus-article-prepare-hook
10976           gnus-visual)
10977       (gnus-summary-select-article nil 'force)))
10978 ;  (gnus-configure-windows 'article)
10979   (gnus-summary-position-point))
10980
10981 (defun gnus-summary-verbose-headers (&optional arg)
10982   "Toggle permanent full header display.
10983 If ARG is a positive number, turn header display on.
10984 If ARG is a negative number, turn header display off."
10985   (interactive "P")
10986   (gnus-set-global-variables)
10987   (gnus-summary-toggle-header arg)
10988   (setq gnus-show-all-headers
10989         (cond ((or (not (numberp arg))
10990                    (zerop arg))
10991                (not gnus-show-all-headers))
10992               ((natnump arg)
10993                t))))
10994
10995 (defun gnus-summary-toggle-header (&optional arg)
10996   "Show the headers if they are hidden, or hide them if they are shown.
10997 If ARG is a positive number, show the entire header.
10998 If ARG is a negative number, hide the unwanted header lines."
10999   (interactive "P")
11000   (gnus-set-global-variables)
11001   (save-excursion
11002     (set-buffer gnus-article-buffer)
11003     (let* ((buffer-read-only nil)
11004            (inhibit-point-motion-hooks t)
11005            (hidden (text-property-any
11006                     (goto-char (point-min)) (search-forward "\n\n")
11007                     'invisible t))
11008            e)
11009       (goto-char (point-min))
11010       (when (search-forward "\n\n" nil t)
11011         (delete-region (point-min) (1- (point))))
11012       (goto-char (point-min))
11013       (save-excursion
11014         (set-buffer gnus-original-article-buffer)
11015         (goto-char (point-min))
11016         (setq e (1- (or (search-forward "\n\n" nil t) (point-max)))))
11017       (insert-buffer-substring gnus-original-article-buffer 1 e)
11018       (let ((gnus-inhibit-hiding t))
11019         (run-hooks 'gnus-article-display-hook))
11020       (if (or (not hidden) (and (numberp arg) (< arg 0)))
11021           (gnus-article-hide-headers)))))
11022
11023 (defun gnus-summary-show-all-headers ()
11024   "Make all header lines visible."
11025   (interactive)
11026   (gnus-set-global-variables)
11027   (gnus-article-show-all-headers))
11028
11029 (defun gnus-summary-toggle-mime (&optional arg)
11030   "Toggle MIME processing.
11031 If ARG is a positive number, turn MIME processing on."
11032   (interactive "P")
11033   (gnus-set-global-variables)
11034   (setq gnus-show-mime
11035         (if (null arg) (not gnus-show-mime)
11036           (> (prefix-numeric-value arg) 0)))
11037   (gnus-summary-select-article t 'force))
11038
11039 (defun gnus-summary-caesar-message (&optional arg)
11040   "Caesar rotate the current article by 13.
11041 The numerical prefix specifies how manu places to rotate each letter
11042 forward."
11043   (interactive "P")
11044   (gnus-set-global-variables)
11045   (gnus-summary-select-article)
11046   (let ((mail-header-separator ""))
11047     (gnus-eval-in-buffer-window
11048      gnus-article-buffer
11049      (save-restriction
11050        (widen)
11051        (let ((start (window-start)))
11052          (news-caesar-buffer-body arg)
11053          (set-window-start (get-buffer-window (current-buffer)) start))))))
11054
11055 (defun gnus-summary-stop-page-breaking ()
11056   "Stop page breaking in the current article."
11057   (interactive)
11058   (gnus-set-global-variables)
11059   (gnus-summary-select-article)
11060   (gnus-eval-in-buffer-window gnus-article-buffer (widen)))
11061
11062 (defun gnus-summary-move-article (&optional n to-newsgroup select-method action)
11063   "Move the current article to a different newsgroup.
11064 If N is a positive number, move the N next articles.
11065 If N is a negative number, move the N previous articles.
11066 If N is nil and any articles have been marked with the process mark,
11067 move those articles instead.
11068 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11069 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
11070 re-spool using this method.
11071
11072 For this function to work, both the current newsgroup and the
11073 newsgroup that you want to move to have to support the `request-move'
11074 and `request-accept' functions."
11075   (interactive "P")
11076   (unless action (setq action 'move))
11077   (gnus-set-global-variables)
11078   ;; Check whether the source group supports the required functions.
11079   (cond ((and (eq action 'move)
11080               (not (gnus-check-backend-function
11081                     'request-move-article gnus-newsgroup-name)))
11082          (error "The current group does not support article moving"))
11083         ((and (eq action 'crosspost)
11084               (not (gnus-check-backend-function
11085                     'request-replace-article gnus-newsgroup-name)))
11086          (error "The current group does not support article editing")))
11087   (let ((articles (gnus-summary-work-articles n))
11088         (prefix (gnus-group-real-prefix gnus-newsgroup-name))
11089         (names '((move "move" "Moving")
11090                  (copy "copy" "Copying")
11091                  (crosspost "crosspost" "Crossposting")))
11092         (copy-buf (save-excursion
11093                     (nnheader-set-temp-buffer " *copy article*")))
11094         art-group to-method new-xref article)
11095     (unless (assq action names)
11096       (error "Unknown action %s" action))
11097     ;; Read the newsgroup name.
11098     (when (and (not to-newsgroup)
11099                (not select-method))
11100       (setq to-newsgroup
11101             (gnus-read-move-group-name
11102              (cadr (assq action names))
11103              gnus-current-move-group articles prefix))
11104       (set (intern (format "gnus-current-%s-group" action)) to-newsgroup))
11105     (setq to-method (if select-method (list select-method "")
11106                       (gnus-find-method-for-group to-newsgroup)))
11107     ;;(when (equal to-newsgroup gnus-newsgroup-name)
11108     ;;(error "Can't %s to the same group you're already in" action))
11109     ;; Check the method we are to move this article to...
11110     (or (gnus-check-backend-function 'request-accept-article (car to-method))
11111         (error "%s does not support article copying" (car to-method)))
11112     (or (gnus-check-server to-method)
11113         (error "Can't open server %s" (car to-method)))
11114     (gnus-message 6 "%s to %s: %s..."
11115                   (caddr (assq action names))
11116                   (or select-method to-newsgroup) articles)
11117     (while articles
11118       (setq article (pop articles))
11119       (setq
11120        art-group
11121        (cond
11122         ;; Move the article.
11123         ((eq action 'move)
11124          (gnus-request-move-article
11125           article                       ; Article to move
11126           gnus-newsgroup-name           ; From newsgrouo
11127           (nth 1 (gnus-find-method-for-group
11128                   gnus-newsgroup-name)) ; Server
11129           (list 'gnus-request-accept-article
11130                 (if select-method
11131                     (list 'quote select-method)
11132                   to-newsgroup)
11133                 (not articles))         ; Accept form
11134           (not articles)))              ; Only save nov last time
11135         ;; Copy the article.
11136         ((eq action 'copy)
11137          (save-excursion
11138            (set-buffer copy-buf)
11139            (gnus-request-article-this-buffer article gnus-newsgroup-name)
11140            (gnus-request-accept-article
11141             (if select-method select-method to-newsgroup)
11142             (not articles))))
11143         ;; Crosspost the article.
11144         ((eq action 'crosspost)
11145          (let ((xref (mail-header-xref (gnus-summary-article-header article))))
11146            (setq new-xref (concat gnus-newsgroup-name ":" article))
11147            (if (and xref (not (string= xref "")))
11148                (progn
11149                  (when (string-match "^Xref: " xref)
11150                    (setq xref (substring xref (match-end 0))))
11151                  (setq new-xref (concat xref " " new-xref)))
11152              (setq new-xref (concat (system-name) " " new-xref)))
11153            (save-excursion
11154              (set-buffer copy-buf)
11155              (gnus-request-article-this-buffer article gnus-newsgroup-name)
11156              (nnheader-replace-header "xref" new-xref)
11157              (gnus-request-accept-article
11158               (if select-method select-method to-newsgroup)
11159               (not articles)))))))
11160       (if (not art-group)
11161           (gnus-message 1 "Couldn't %s article %s"
11162                         (cadr (assq action names)) article)
11163         (let* ((entry
11164                 (or
11165                  (gnus-gethash (car art-group) gnus-newsrc-hashtb)
11166                  (gnus-gethash
11167                   (gnus-group-prefixed-name
11168                    (car art-group)
11169                    (if select-method (list select-method "")
11170                      (gnus-find-method-for-group to-newsgroup)))
11171                   gnus-newsrc-hashtb)))
11172                (info (nth 2 entry)))
11173           ;; Update the group that has been moved to.
11174           (when (and info
11175                      (memq action '(move copy)))
11176             (unless (memq article gnus-newsgroup-unreads)
11177               (gnus-info-set-read
11178                info (gnus-add-to-range (gnus-info-read info)
11179                                        (list (cdr art-group)))))
11180
11181             ;; Copy any marks over to the new group.
11182             (let ((marks gnus-article-mark-lists)
11183                   (to-article (cdr art-group)))
11184
11185               ;; See whether the article is to be put in the cache.
11186               (when gnus-use-cache
11187                 (gnus-cache-possibly-enter-article
11188                  (gnus-info-group info) to-article
11189                  (let ((header (copy-sequence
11190                                 (gnus-summary-article-header article))))
11191                    (mail-header-set-number header to-article)
11192                    header)
11193                  (memq article gnus-newsgroup-marked)
11194                  (memq article gnus-newsgroup-dormant)
11195                  (memq article gnus-newsgroup-unreads)))
11196
11197               (while marks
11198                 (when (memq article (symbol-value
11199                                      (intern (format "gnus-newsgroup-%s"
11200                                                      (caar marks)))))
11201                   (gnus-add-marked-articles
11202                    (gnus-info-group info) (cdar marks)
11203                    (list to-article) info))
11204                 (setq marks (cdr marks)))))
11205
11206           ;; Update the Xref header in this article to point to
11207           ;; the new crossposted article we have just created.
11208           (when (eq action 'crosspost)
11209             (save-excursion
11210               (set-buffer copy-buf)
11211               (gnus-request-article-this-buffer article gnus-newsgroup-name)
11212               (nnheader-replace-header
11213                "xref" (concat new-xref " " (gnus-group-prefixed-name
11214                                             (car art-group) to-method)
11215                               ":" (cdr art-group)))
11216               (gnus-request-replace-article
11217                article gnus-newsgroup-name (current-buffer)))))
11218
11219         (gnus-summary-goto-subject article)
11220         (when (eq action 'move)
11221           (gnus-summary-mark-article article gnus-canceled-mark)))
11222       (gnus-summary-remove-process-mark article))
11223     (gnus-kill-buffer copy-buf)
11224     (gnus-summary-position-point)
11225     (gnus-set-mode-line 'summary)))
11226
11227 (defun gnus-summary-copy-article (&optional n to-newsgroup select-method)
11228   "Move the current article to a different newsgroup.
11229 If TO-NEWSGROUP is string, do not prompt for a newsgroup to move to.
11230 If SELECT-METHOD is symbol, do not move to a specific newsgroup, but
11231 re-spool using this method."
11232   (interactive "P")
11233   (gnus-summary-move-article n nil nil 'copy))
11234
11235 (defun gnus-summary-crosspost-article (&optional n)
11236   "Crosspost the current article to some other group."
11237   (interactive "P")
11238   (gnus-summary-move-article n nil nil 'crosspost))
11239
11240 (defun gnus-summary-respool-article (&optional n respool-method)
11241   "Respool the current article.
11242 The article will be squeezed through the mail spooling process again,
11243 which means that it will be put in some mail newsgroup or other
11244 depending on `nnmail-split-methods'.
11245 If N is a positive number, respool the N next articles.
11246 If N is a negative number, respool the N previous articles.
11247 If N is nil and any articles have been marked with the process mark,
11248 respool those articles instead.
11249
11250 Respooling can be done both from mail groups and \"real\" newsgroups.
11251 In the former case, the articles in question will be moved from the
11252 current group into whatever groups they are destined to.  In the
11253 latter case, they will be copied into the relevant groups."
11254   (interactive "P")
11255   (gnus-set-global-variables)
11256   (let ((respool-methods (gnus-methods-using 'respool))
11257         (methname
11258          (symbol-name (car (gnus-find-method-for-group gnus-newsgroup-name)))))
11259     (or respool-method
11260         (setq respool-method
11261               (completing-read
11262                "What method do you want to use when respooling? "
11263                respool-methods nil t methname)))
11264     (or (string= respool-method "")
11265         (if (assoc (symbol-name
11266                     (car (gnus-find-method-for-group gnus-newsgroup-name)))
11267                    respool-methods)
11268             (gnus-summary-move-article n nil (intern respool-method))
11269           (gnus-summary-copy-article n nil (intern respool-method))))))
11270
11271 (defun gnus-summary-import-article (file)
11272   "Import a random file into a mail newsgroup."
11273   (interactive "fImport file: ")
11274   (gnus-set-global-variables)
11275   (let ((group gnus-newsgroup-name)
11276         (now (current-time))
11277         atts lines)
11278     (or (gnus-check-backend-function 'request-accept-article group)
11279         (error "%s does not support article importing" group))
11280     (or (file-readable-p file)
11281         (not (file-regular-p file))
11282         (error "Can't read %s" file))
11283     (save-excursion
11284       (set-buffer (get-buffer-create " *import file*"))
11285       (buffer-disable-undo (current-buffer))
11286       (erase-buffer)
11287       (insert-file-contents file)
11288       (goto-char (point-min))
11289       (unless (nnheader-article-p)
11290         ;; This doesn't look like an article, so we fudge some headers.
11291         (setq atts (file-attributes file)
11292               lines (count-lines (point-min) (point-max)))
11293         (insert "From: " (read-string "From: ") "\n"
11294                 "Subject: " (read-string "Subject: ") "\n"
11295                 "Date: " (timezone-make-date-arpa-standard
11296                           (current-time-string (nth 5 atts))
11297                           (current-time-zone now)
11298                           (current-time-zone now)) "\n"
11299                 "Message-ID: " (gnus-inews-message-id) "\n"
11300                 "Lines: " (int-to-string lines) "\n"
11301                 "Chars: " (int-to-string (nth 7 atts)) "\n\n"))
11302       (gnus-request-accept-article group t)
11303       (kill-buffer (current-buffer)))))
11304
11305 (defun gnus-summary-expire-articles ()
11306   "Expire all articles that are marked as expirable in the current group."
11307   (interactive)
11308   (gnus-set-global-variables)
11309   (when (gnus-check-backend-function
11310          'request-expire-articles gnus-newsgroup-name)
11311     ;; This backend supports expiry.
11312     (let* ((total (gnus-group-total-expirable-p gnus-newsgroup-name))
11313            (expirable (if total
11314                           (gnus-list-of-read-articles gnus-newsgroup-name)
11315                         (setq gnus-newsgroup-expirable
11316                               (sort gnus-newsgroup-expirable '<))))
11317            (expiry-wait (gnus-group-get-parameter
11318                          gnus-newsgroup-name 'expiry-wait))
11319            es)
11320       (when expirable
11321         ;; There are expirable articles in this group, so we run them
11322         ;; through the expiry process.
11323         (gnus-message 6 "Expiring articles...")
11324         ;; The list of articles that weren't expired is returned.
11325         (if expiry-wait
11326             (let ((nnmail-expiry-wait-function nil)
11327                   (nnmail-expiry-wait expiry-wait))
11328               (setq es (gnus-request-expire-articles
11329                         expirable gnus-newsgroup-name)))
11330           (setq es (gnus-request-expire-articles
11331                     expirable gnus-newsgroup-name)))
11332         (or total (setq gnus-newsgroup-expirable es))
11333         ;; We go through the old list of expirable, and mark all
11334         ;; really expired articles as nonexistent.
11335         (unless (eq es expirable)       ;If nothing was expired, we don't mark.
11336           (let ((gnus-use-cache nil))
11337             (while expirable
11338               (unless (memq (car expirable) es)
11339                 (when (gnus-data-find (car expirable))
11340                   (gnus-summary-mark-article
11341                    (car expirable) gnus-canceled-mark)))
11342               (setq expirable (cdr expirable)))))
11343         (gnus-message 6 "Expiring articles...done")))))
11344
11345 (defun gnus-summary-expire-articles-now ()
11346   "Expunge all expirable articles in the current group.
11347 This means that *all* articles that are marked as expirable will be
11348 deleted forever, right now."
11349   (interactive)
11350   (gnus-set-global-variables)
11351   (or gnus-expert-user
11352       (gnus-y-or-n-p
11353        "Are you really, really, really sure you want to expunge? ")
11354       (error "Phew!"))
11355   (let ((nnmail-expiry-wait 'immediate)
11356         (nnmail-expiry-wait-function nil))
11357     (gnus-summary-expire-articles)))
11358
11359 ;; Suggested by Jack Vinson <vinson@unagi.cis.upenn.edu>.
11360 (defun gnus-summary-delete-article (&optional n)
11361   "Delete the N next (mail) articles.
11362 This command actually deletes articles.  This is not a marking
11363 command.  The article will disappear forever from your life, never to
11364 return.
11365 If N is negative, delete backwards.
11366 If N is nil and articles have been marked with the process mark,
11367 delete these instead."
11368   (interactive "P")
11369   (gnus-set-global-variables)
11370   (or (gnus-check-backend-function 'request-expire-articles
11371                                    gnus-newsgroup-name)
11372       (error "The current newsgroup does not support article deletion."))
11373   ;; Compute the list of articles to delete.
11374   (let ((articles (gnus-summary-work-articles n))
11375         not-deleted)
11376     (if (and gnus-novice-user
11377              (not (gnus-y-or-n-p
11378                    (format "Do you really want to delete %s forever? "
11379                            (if (> (length articles) 1) "these articles"
11380                              "this article")))))
11381         ()
11382       ;; Delete the articles.
11383       (setq not-deleted (gnus-request-expire-articles
11384                          articles gnus-newsgroup-name 'force))
11385       (while articles
11386         (gnus-summary-remove-process-mark (car articles))
11387         ;; The backend might not have been able to delete the article
11388         ;; after all.
11389         (or (memq (car articles) not-deleted)
11390             (gnus-summary-mark-article (car articles) gnus-canceled-mark))
11391         (setq articles (cdr articles))))
11392     (gnus-summary-position-point)
11393     (gnus-set-mode-line 'summary)
11394     not-deleted))
11395
11396 (defun gnus-summary-edit-article (&optional force)
11397   "Enter into a buffer and edit the current article.
11398 This will have permanent effect only in mail groups.
11399 If FORCE is non-nil, allow editing of articles even in read-only
11400 groups."
11401   (interactive "P")
11402   (save-excursion
11403     (set-buffer gnus-summary-buffer)
11404     (gnus-set-global-variables)
11405     (when (and (not force)
11406                (gnus-group-read-only-p))
11407       (error "The current newsgroup does not support article editing."))
11408     (gnus-summary-select-article t nil t)
11409     (gnus-configure-windows 'article)
11410     (select-window (get-buffer-window gnus-article-buffer))
11411     (gnus-message 6 "C-c C-c to end edits")
11412     (setq buffer-read-only nil)
11413     (text-mode)
11414     (use-local-map (copy-keymap (current-local-map)))
11415     (local-set-key "\C-c\C-c" 'gnus-summary-edit-article-done)
11416     (buffer-enable-undo)
11417     (widen)
11418     (goto-char (point-min))
11419     (search-forward "\n\n" nil t)))
11420
11421 (defun gnus-summary-edit-article-done ()
11422   "Make edits to the current article permanent."
11423   (interactive)
11424   (if (gnus-group-read-only-p)
11425       (progn
11426         (gnus-summary-edit-article-postpone)
11427         (gnus-message
11428          1 "The current newsgroup does not support article editing.")
11429         (ding))
11430     (let ((buf (buffer-substring-no-properties (point-min) (point-max))))
11431       (erase-buffer)
11432       (insert buf)
11433       (if (not (gnus-request-replace-article
11434                 (cdr gnus-article-current) (car gnus-article-current)
11435                 (current-buffer)))
11436           (error "Couldn't replace article.")
11437         (gnus-article-mode)
11438         (use-local-map gnus-article-mode-map)
11439         (setq buffer-read-only t)
11440         (buffer-disable-undo (current-buffer))
11441         (gnus-configure-windows 'summary)
11442         (gnus-summary-update-article (cdr gnus-article-current))
11443         (when gnus-use-cache
11444           (gnus-cache-update-article 
11445            (cdr gnus-article-current) (car gnus-article-current))))
11446       (run-hooks 'gnus-article-display-hook)
11447       (and (gnus-visual-p 'summary-highlight 'highlight)
11448            (run-hooks 'gnus-visual-mark-article-hook)))))
11449
11450 (defun gnus-summary-edit-article-postpone ()
11451   "Postpone changes to the current article."
11452   (interactive)
11453   (gnus-article-mode)
11454   (use-local-map gnus-article-mode-map)
11455   (setq buffer-read-only t)
11456   (buffer-disable-undo (current-buffer))
11457   (gnus-configure-windows 'summary)
11458   (and (gnus-visual-p 'summary-highlight 'highlight)
11459        (run-hooks 'gnus-visual-mark-article-hook)))
11460
11461 (defun gnus-summary-respool-query ()
11462   "Query where the respool algorithm would put this article."
11463   (interactive)
11464   (gnus-set-global-variables)
11465   (gnus-summary-select-article)
11466   (save-excursion
11467     (set-buffer gnus-article-buffer)
11468     (save-restriction
11469       (goto-char (point-min))
11470       (search-forward "\n\n")
11471       (narrow-to-region (point-min) (point))
11472       (pp-eval-expression
11473        (list 'quote (mapcar 'car (nnmail-article-group 'identity)))))))
11474
11475 ;; Summary score commands.
11476
11477 ;; Suggested by boubaker@cenatls.cena.dgac.fr.
11478
11479 (defun gnus-summary-raise-score (n)
11480   "Raise the score of the current article by N."
11481   (interactive "p")
11482   (gnus-set-global-variables)
11483   (gnus-summary-set-score (+ (gnus-summary-article-score) n)))
11484
11485 (defun gnus-summary-set-score (n)
11486   "Set the score of the current article to N."
11487   (interactive "p")
11488   (gnus-set-global-variables)
11489   (save-excursion
11490     (gnus-summary-show-thread)
11491     (let ((buffer-read-only nil))
11492       ;; Set score.
11493       (gnus-summary-update-mark
11494        (if (= n (or gnus-summary-default-score 0)) ? 
11495          (if (< n (or gnus-summary-default-score 0))
11496              gnus-score-below-mark gnus-score-over-mark)) 'score))
11497     (let* ((article (gnus-summary-article-number))
11498            (score (assq article gnus-newsgroup-scored)))
11499       (if score (setcdr score n)
11500         (setq gnus-newsgroup-scored
11501               (cons (cons article n) gnus-newsgroup-scored))))
11502     (gnus-summary-update-line)))
11503
11504 (defun gnus-summary-current-score ()
11505   "Return the score of the current article."
11506   (interactive)
11507   (gnus-set-global-variables)
11508   (message "%s" (gnus-summary-article-score)))
11509
11510 ;; Summary marking commands.
11511
11512 (defun gnus-summary-kill-same-subject-and-select (&optional unmark)
11513   "Mark articles which has the same subject as read, and then select the next.
11514 If UNMARK is positive, remove any kind of mark.
11515 If UNMARK is negative, tick articles."
11516   (interactive "P")
11517   (gnus-set-global-variables)
11518   (if unmark
11519       (setq unmark (prefix-numeric-value unmark)))
11520   (let ((count
11521          (gnus-summary-mark-same-subject
11522           (gnus-summary-article-subject) unmark)))
11523     ;; Select next unread article.  If auto-select-same mode, should
11524     ;; select the first unread article.
11525     (gnus-summary-next-article t (and gnus-auto-select-same
11526                                       (gnus-summary-article-subject)))
11527     (gnus-message 7 "%d article%s marked as %s"
11528                   count (if (= count 1) " is" "s are")
11529                   (if unmark "unread" "read"))))
11530
11531 (defun gnus-summary-kill-same-subject (&optional unmark)
11532   "Mark articles which has the same subject as read.
11533 If UNMARK is positive, remove any kind of mark.
11534 If UNMARK is negative, tick articles."
11535   (interactive "P")
11536   (gnus-set-global-variables)
11537   (if unmark
11538       (setq unmark (prefix-numeric-value unmark)))
11539   (let ((count
11540          (gnus-summary-mark-same-subject
11541           (gnus-summary-article-subject) unmark)))
11542     ;; If marked as read, go to next unread subject.
11543     (if (null unmark)
11544         ;; Go to next unread subject.
11545         (gnus-summary-next-subject 1 t))
11546     (gnus-message 7 "%d articles are marked as %s"
11547                   count (if unmark "unread" "read"))))
11548
11549 (defun gnus-summary-mark-same-subject (subject &optional unmark)
11550   "Mark articles with same SUBJECT as read, and return marked number.
11551 If optional argument UNMARK is positive, remove any kinds of marks.
11552 If optional argument UNMARK is negative, mark articles as unread instead."
11553   (let ((count 1))
11554     (save-excursion
11555       (cond
11556        ((null unmark)                   ; Mark as read.
11557         (while (and
11558                 (progn
11559                   (gnus-summary-mark-article-as-read gnus-killed-mark)
11560                   (gnus-summary-show-thread) t)
11561                 (gnus-summary-find-subject subject))
11562           (setq count (1+ count))))
11563        ((> unmark 0)                    ; Tick.
11564         (while (and
11565                 (progn
11566                   (gnus-summary-mark-article-as-unread gnus-ticked-mark)
11567                   (gnus-summary-show-thread) t)
11568                 (gnus-summary-find-subject subject))
11569           (setq count (1+ count))))
11570        (t                               ; Mark as unread.
11571         (while (and
11572                 (progn
11573                   (gnus-summary-mark-article-as-unread gnus-unread-mark)
11574                   (gnus-summary-show-thread) t)
11575                 (gnus-summary-find-subject subject))
11576           (setq count (1+ count)))))
11577       (gnus-set-mode-line 'summary)
11578       ;; Return the number of marked articles.
11579       count)))
11580
11581 (defun gnus-summary-mark-as-processable (n &optional unmark)
11582   "Set the process mark on the next N articles.
11583 If N is negative, mark backward instead.  If UNMARK is non-nil, remove
11584 the process mark instead.  The difference between N and the actual
11585 number of articles marked is returned."
11586   (interactive "p")
11587   (gnus-set-global-variables)
11588   (let ((backward (< n 0))
11589         (n (abs n)))
11590     (while (and
11591             (> n 0)
11592             (if unmark
11593                 (gnus-summary-remove-process-mark
11594                  (gnus-summary-article-number))
11595               (gnus-summary-set-process-mark (gnus-summary-article-number)))
11596             (zerop (gnus-summary-next-subject (if backward -1 1) nil t)))
11597       (setq n (1- n)))
11598     (if (/= 0 n) (gnus-message 7 "No more articles"))
11599     (gnus-summary-recenter)
11600     (gnus-summary-position-point)
11601     n))
11602
11603 (defun gnus-summary-unmark-as-processable (n)
11604   "Remove the process mark from the next N articles.
11605 If N is negative, mark backward instead.  The difference between N and
11606 the actual number of articles marked is returned."
11607   (interactive "p")
11608   (gnus-set-global-variables)
11609   (gnus-summary-mark-as-processable n t))
11610
11611 (defun gnus-summary-unmark-all-processable ()
11612   "Remove the process mark from all articles."
11613   (interactive)
11614   (gnus-set-global-variables)
11615   (save-excursion
11616     (while gnus-newsgroup-processable
11617       (gnus-summary-remove-process-mark (car gnus-newsgroup-processable))))
11618   (gnus-summary-position-point))
11619
11620 (defun gnus-summary-mark-as-expirable (n)
11621   "Mark N articles forward as expirable.
11622 If N is negative, mark backward instead.  The difference between N and
11623 the actual number of articles marked is returned."
11624   (interactive "p")
11625   (gnus-set-global-variables)
11626   (gnus-summary-mark-forward n gnus-expirable-mark))
11627
11628 (defun gnus-summary-mark-article-as-replied (article)
11629   "Mark ARTICLE replied and update the summary line."
11630   (setq gnus-newsgroup-replied (cons article gnus-newsgroup-replied))
11631   (let ((buffer-read-only nil))
11632     (when (gnus-summary-goto-subject article)
11633       (gnus-summary-update-secondary-mark article))))
11634
11635 (defun gnus-summary-set-bookmark (article)
11636   "Set a bookmark in current article."
11637   (interactive (list (gnus-summary-article-number)))
11638   (gnus-set-global-variables)
11639   (if (or (not (get-buffer gnus-article-buffer))
11640           (not gnus-current-article)
11641           (not gnus-article-current)
11642           (not (equal gnus-newsgroup-name (car gnus-article-current))))
11643       (error "No current article selected"))
11644   ;; Remove old bookmark, if one exists.
11645   (let ((old (assq article gnus-newsgroup-bookmarks)))
11646     (if old (setq gnus-newsgroup-bookmarks
11647                   (delq old gnus-newsgroup-bookmarks))))
11648   ;; Set the new bookmark, which is on the form
11649   ;; (article-number . line-number-in-body).
11650   (setq gnus-newsgroup-bookmarks
11651         (cons
11652          (cons article
11653                (save-excursion
11654                  (set-buffer gnus-article-buffer)
11655                  (count-lines
11656                   (min (point)
11657                        (save-excursion
11658                          (goto-char (point-min))
11659                          (search-forward "\n\n" nil t)
11660                          (point)))
11661                   (point))))
11662          gnus-newsgroup-bookmarks))
11663   (gnus-message 6 "A bookmark has been added to the current article."))
11664
11665 (defun gnus-summary-remove-bookmark (article)
11666   "Remove the bookmark from the current article."
11667   (interactive (list (gnus-summary-article-number)))
11668   (gnus-set-global-variables)
11669   ;; Remove old bookmark, if one exists.
11670   (let ((old (assq article gnus-newsgroup-bookmarks)))
11671     (if old
11672         (progn
11673           (setq gnus-newsgroup-bookmarks
11674                 (delq old gnus-newsgroup-bookmarks))
11675           (gnus-message 6 "Removed bookmark."))
11676       (gnus-message 6 "No bookmark in current article."))))
11677
11678 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
11679 (defun gnus-summary-mark-as-dormant (n)
11680   "Mark N articles forward as dormant.
11681 If N is negative, mark backward instead.  The difference between N and
11682 the actual number of articles marked is returned."
11683   (interactive "p")
11684   (gnus-set-global-variables)
11685   (gnus-summary-mark-forward n gnus-dormant-mark))
11686
11687 (defun gnus-summary-set-process-mark (article)
11688   "Set the process mark on ARTICLE and update the summary line."
11689   (setq gnus-newsgroup-processable
11690         (cons article
11691               (delq article gnus-newsgroup-processable)))
11692   (when (gnus-summary-goto-subject article)
11693     (gnus-summary-show-thread)
11694     (gnus-summary-update-secondary-mark article)))
11695
11696 (defun gnus-summary-remove-process-mark (article)
11697   "Remove the process mark from ARTICLE and update the summary line."
11698   (setq gnus-newsgroup-processable (delq article gnus-newsgroup-processable))
11699   (when (gnus-summary-goto-subject article)
11700     (gnus-summary-show-thread)
11701     (gnus-summary-update-secondary-mark article)))
11702
11703 (defun gnus-summary-set-saved-mark (article)
11704   "Set the process mark on ARTICLE and update the summary line."
11705   (push article gnus-newsgroup-saved)
11706   (when (gnus-summary-goto-subject article)
11707     (gnus-summary-update-secondary-mark article)))
11708
11709 (defun gnus-summary-mark-forward (n &optional mark no-expire)
11710   "Mark N articles as read forwards.
11711 If N is negative, mark backwards instead.
11712 Mark with MARK.  If MARK is ? , ?! or ??, articles will be
11713 marked as unread.
11714 The difference between N and the actual number of articles marked is
11715 returned."
11716   (interactive "p")
11717   (gnus-set-global-variables)
11718   (let ((backward (< n 0))
11719         (gnus-summary-goto-unread
11720          (and gnus-summary-goto-unread
11721               (not (eq gnus-summary-goto-unread 'never))
11722               (not (memq mark (list gnus-unread-mark
11723                                     gnus-ticked-mark gnus-dormant-mark)))))
11724         (n (abs n))
11725         (mark (or mark gnus-del-mark)))
11726     (while (and (> n 0)
11727                 (gnus-summary-mark-article nil mark no-expire)
11728                 (zerop (gnus-summary-next-subject
11729                         (if backward -1 1)
11730                         (and gnus-summary-goto-unread
11731                              (not (eq gnus-summary-goto-unread 'never)))
11732                         t)))
11733       (setq n (1- n)))
11734     (if (/= 0 n) (gnus-message 7 "No more %sarticles" (if mark "" "unread ")))
11735     (gnus-summary-recenter)
11736     (gnus-summary-position-point)
11737     (gnus-set-mode-line 'summary)
11738     n))
11739
11740 (defun gnus-summary-mark-article-as-read (mark)
11741   "Mark the current article quickly as read with MARK."
11742   (let ((article (gnus-summary-article-number)))
11743     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11744     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11745     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11746     (setq gnus-newsgroup-reads
11747           (cons (cons article mark) gnus-newsgroup-reads))
11748     ;; Possibly remove from cache, if that is used.
11749     (and gnus-use-cache (gnus-cache-enter-remove-article article))
11750     ;; Allow the backend to change the mark.
11751     (setq mark (gnus-request-update-mark gnus-newsgroup-name article mark))
11752     ;; Check for auto-expiry.
11753     (when (and gnus-newsgroup-auto-expire
11754                (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11755                    (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11756                    (= mark gnus-ancient-mark)
11757                    (= mark gnus-read-mark) (= mark gnus-souped-mark)))
11758       (setq mark gnus-expirable-mark)
11759       (push article gnus-newsgroup-expirable))
11760     ;; Set the mark in the buffer.
11761     (gnus-summary-update-mark mark 'unread)
11762     t))
11763
11764 (defun gnus-summary-mark-article-as-unread (mark)
11765   "Mark the current article quickly as unread with MARK."
11766   (let ((article (gnus-summary-article-number)))
11767     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11768     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11769     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11770     (setq gnus-newsgroup-reads (delq article gnus-newsgroup-reads))
11771     (cond ((= mark gnus-ticked-mark)
11772            (push article gnus-newsgroup-marked))
11773           ((= mark gnus-dormant-mark)
11774            (push article gnus-newsgroup-dormant))
11775           (t
11776            (push article gnus-newsgroup-unreads)))
11777     (setq gnus-newsgroup-reads
11778           (delq (assq article gnus-newsgroup-reads)
11779                 gnus-newsgroup-reads))
11780
11781     ;; See whether the article is to be put in the cache.
11782     (and gnus-use-cache
11783          (vectorp (gnus-summary-article-header article))
11784          (save-excursion
11785            (gnus-cache-possibly-enter-article
11786             gnus-newsgroup-name article
11787             (gnus-summary-article-header article)
11788             (= mark gnus-ticked-mark)
11789             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11790
11791     ;; Fix the mark.
11792     (gnus-summary-update-mark mark 'unread)
11793     t))
11794
11795 (defun gnus-summary-mark-article (&optional article mark no-expire)
11796   "Mark ARTICLE with MARK.  MARK can be any character.
11797 Four MARK strings are reserved: `? ' (unread), `?!' (ticked),
11798 `??' (dormant) and `?E' (expirable).
11799 If MARK is nil, then the default character `?D' is used.
11800 If ARTICLE is nil, then the article on the current line will be
11801 marked."
11802   ;; The mark might be a string.
11803   (and (stringp mark)
11804        (setq mark (aref mark 0)))
11805   ;; If no mark is given, then we check auto-expiring.
11806   (and (not no-expire)
11807        gnus-newsgroup-auto-expire
11808        (or (not mark)
11809            (and (numberp mark)
11810                 (or (= mark gnus-killed-mark) (= mark gnus-del-mark)
11811                     (= mark gnus-catchup-mark) (= mark gnus-low-score-mark)
11812                     (= mark gnus-read-mark) (= mark gnus-souped-mark))))
11813        (setq mark gnus-expirable-mark))
11814   (let* ((mark (or mark gnus-del-mark))
11815          (article (or article (gnus-summary-article-number))))
11816     (or article (error "No article on current line"))
11817     (if (or (= mark gnus-unread-mark)
11818             (= mark gnus-ticked-mark)
11819             (= mark gnus-dormant-mark))
11820         (gnus-mark-article-as-unread article mark)
11821       (gnus-mark-article-as-read article mark))
11822
11823     ;; See whether the article is to be put in the cache.
11824     (and gnus-use-cache
11825          (not (= mark gnus-canceled-mark))
11826          (vectorp (gnus-summary-article-header article))
11827          (save-excursion
11828            (gnus-cache-possibly-enter-article
11829             gnus-newsgroup-name article
11830             (gnus-summary-article-header article)
11831             (= mark gnus-ticked-mark)
11832             (= mark gnus-dormant-mark) (= mark gnus-unread-mark))))
11833
11834     (if (gnus-summary-goto-subject article nil t)
11835         (let ((buffer-read-only nil))
11836           (gnus-summary-show-thread)
11837           ;; Fix the mark.
11838           (gnus-summary-update-mark mark 'unread)
11839           t))))
11840
11841 (defun gnus-summary-update-secondary-mark (article)
11842   "Update the secondary (read, process, cache) mark."
11843   (gnus-summary-update-mark
11844    (cond ((memq article gnus-newsgroup-processable)
11845           gnus-process-mark)
11846          ((memq article gnus-newsgroup-cached)
11847           gnus-cached-mark)
11848          ((memq article gnus-newsgroup-replied)
11849           gnus-replied-mark)
11850          ((memq article gnus-newsgroup-saved)
11851           gnus-saved-mark)
11852          (t gnus-unread-mark))
11853    'replied)
11854   (when (gnus-visual-p 'summary-highlight 'highlight)
11855     (run-hooks 'gnus-summary-update-hook))
11856   t)
11857
11858 (defun gnus-summary-update-mark (mark type)
11859   (beginning-of-line)
11860   (let ((forward (cdr (assq type gnus-summary-mark-positions)))
11861         (buffer-read-only nil))
11862     (when forward
11863       ;; Go to the right position on the line.
11864       (forward-char forward)
11865       ;; Replace the old mark with the new mark.
11866       (subst-char-in-region (point) (1+ (point)) (following-char) mark)
11867       ;; Optionally update the marks by some user rule.
11868       (when (eq type 'unread)
11869         (gnus-data-set-mark
11870          (gnus-data-find (gnus-summary-article-number)) mark)
11871         (gnus-summary-update-line (eq mark gnus-unread-mark))))))
11872
11873 (defun gnus-mark-article-as-read (article &optional mark)
11874   "Enter ARTICLE in the pertinent lists and remove it from others."
11875   ;; Make the article expirable.
11876   (let ((mark (or mark gnus-del-mark)))
11877     (if (= mark gnus-expirable-mark)
11878         (setq gnus-newsgroup-expirable (cons article gnus-newsgroup-expirable))
11879       (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable)))
11880     ;; Remove from unread and marked lists.
11881     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11882     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11883     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11884     (push (cons article mark) gnus-newsgroup-reads)
11885     ;; Possibly remove from cache, if that is used.
11886     (when gnus-use-cache
11887       (gnus-cache-enter-remove-article article))))
11888
11889 (defun gnus-mark-article-as-unread (article &optional mark)
11890   "Enter ARTICLE in the pertinent lists and remove it from others."
11891   (let ((mark (or mark gnus-ticked-mark)))
11892     (setq gnus-newsgroup-marked (delq article gnus-newsgroup-marked))
11893     (setq gnus-newsgroup-dormant (delq article gnus-newsgroup-dormant))
11894     (setq gnus-newsgroup-expirable (delq article gnus-newsgroup-expirable))
11895     (setq gnus-newsgroup-unreads (delq article gnus-newsgroup-unreads))
11896     (cond ((= mark gnus-ticked-mark)
11897            (push article gnus-newsgroup-marked))
11898           ((= mark gnus-dormant-mark)
11899            (push article gnus-newsgroup-dormant))
11900           (t
11901            (push article gnus-newsgroup-unreads)))
11902     (setq gnus-newsgroup-reads
11903           (delq (assq article gnus-newsgroup-reads)
11904                 gnus-newsgroup-reads))))
11905
11906 (defalias 'gnus-summary-mark-as-unread-forward
11907   'gnus-summary-tick-article-forward)
11908 (make-obsolete 'gnus-summary-mark-as-unread-forward
11909                'gnus-summary-tick-article-forward)
11910 (defun gnus-summary-tick-article-forward (n)
11911   "Tick N articles forwards.
11912 If N is negative, tick backwards instead.
11913 The difference between N and the number of articles ticked is returned."
11914   (interactive "p")
11915   (gnus-summary-mark-forward n gnus-ticked-mark))
11916
11917 (defalias 'gnus-summary-mark-as-unread-backward
11918   'gnus-summary-tick-article-backward)
11919 (make-obsolete 'gnus-summary-mark-as-unread-backward
11920                'gnus-summary-tick-article-backward)
11921 (defun gnus-summary-tick-article-backward (n)
11922   "Tick N articles backwards.
11923 The difference between N and the number of articles ticked is returned."
11924   (interactive "p")
11925   (gnus-summary-mark-forward (- n) gnus-ticked-mark))
11926
11927 (defalias 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
11928 (make-obsolete 'gnus-summary-mark-as-unread 'gnus-summary-tick-article)
11929 (defun gnus-summary-tick-article (&optional article clear-mark)
11930   "Mark current article as unread.
11931 Optional 1st argument ARTICLE specifies article number to be marked as unread.
11932 Optional 2nd argument CLEAR-MARK remove any kinds of mark."
11933   (interactive)
11934   (gnus-summary-mark-article article (if clear-mark gnus-unread-mark
11935                                        gnus-ticked-mark)))
11936
11937 (defun gnus-summary-mark-as-read-forward (n)
11938   "Mark N articles as read forwards.
11939 If N is negative, mark backwards instead.
11940 The difference between N and the actual number of articles marked is
11941 returned."
11942   (interactive "p")
11943   (gnus-summary-mark-forward n gnus-del-mark t))
11944
11945 (defun gnus-summary-mark-as-read-backward (n)
11946   "Mark the N articles as read backwards.
11947 The difference between N and the actual number of articles marked is
11948 returned."
11949   (interactive "p")
11950   (gnus-summary-mark-forward (- n) gnus-del-mark t))
11951
11952 (defun gnus-summary-mark-as-read (&optional article mark)
11953   "Mark current article as read.
11954 ARTICLE specifies the article to be marked as read.
11955 MARK specifies a string to be inserted at the beginning of the line."
11956   (gnus-summary-mark-article article mark))
11957
11958 (defun gnus-summary-clear-mark-forward (n)
11959   "Clear marks from N articles forward.
11960 If N is negative, clear backward instead.
11961 The difference between N and the number of marks cleared is returned."
11962   (interactive "p")
11963   (gnus-summary-mark-forward n gnus-unread-mark))
11964
11965 (defun gnus-summary-clear-mark-backward (n)
11966   "Clear marks from N articles backward.
11967 The difference between N and the number of marks cleared is returned."
11968   (interactive "p")
11969   (gnus-summary-mark-forward (- n) gnus-unread-mark))
11970
11971 (defun gnus-summary-mark-unread-as-read ()
11972   "Intended to be used by `gnus-summary-mark-article-hook'."
11973   (when (memq gnus-current-article gnus-newsgroup-unreads)
11974     (gnus-summary-mark-article gnus-current-article gnus-read-mark)))
11975
11976 (defun gnus-summary-mark-read-and-unread-as-read ()
11977   "Intended to be used by `gnus-summary-mark-article-hook'."
11978   (let ((mark (gnus-summary-article-mark)))
11979     (when (or (gnus-unread-mark-p mark)
11980               (gnus-read-mark-p mark))
11981       (gnus-summary-mark-article gnus-current-article gnus-read-mark))))
11982
11983 (defun gnus-summary-mark-region-as-read (point mark all)
11984   "Mark all unread articles between point and mark as read.
11985 If given a prefix, mark all articles between point and mark as read,
11986 even ticked and dormant ones."
11987   (interactive "r\nP")
11988   (save-excursion
11989     (let (article)
11990       (goto-char point)
11991       (beginning-of-line)
11992       (while (and
11993               (< (point) mark)
11994               (progn
11995                 (when (or all
11996                           (memq (setq article (gnus-summary-article-number))
11997                                 gnus-newsgroup-unreads))
11998                   (gnus-summary-mark-article article gnus-del-mark))
11999                 t)
12000               (gnus-summary-find-next))))))
12001
12002 (defun gnus-summary-mark-below (score mark)
12003   "Mark articles with score less than SCORE with MARK."
12004   (interactive "P\ncMark: ")
12005   (gnus-set-global-variables)
12006   (setq score (if score
12007                   (prefix-numeric-value score)
12008                 (or gnus-summary-default-score 0)))
12009   (save-excursion
12010     (set-buffer gnus-summary-buffer)
12011     (goto-char (point-min))
12012     (while 
12013         (progn
12014           (and (< (gnus-summary-article-score) score)
12015                (gnus-summary-mark-article nil mark))
12016           (gnus-summary-find-next)))))
12017
12018 (defun gnus-summary-kill-below (&optional score)
12019   "Mark articles with score below SCORE as read."
12020   (interactive "P")
12021   (gnus-set-global-variables)
12022   (gnus-summary-mark-below score gnus-killed-mark))
12023
12024 (defun gnus-summary-clear-above (&optional score)
12025   "Clear all marks from articles with score above SCORE."
12026   (interactive "P")
12027   (gnus-set-global-variables)
12028   (gnus-summary-mark-above score gnus-unread-mark))
12029
12030 (defun gnus-summary-tick-above (&optional score)
12031   "Tick all articles with score above SCORE."
12032   (interactive "P")
12033   (gnus-set-global-variables)
12034   (gnus-summary-mark-above score gnus-ticked-mark))
12035
12036 (defun gnus-summary-mark-above (score mark)
12037   "Mark articles with score over SCORE with MARK."
12038   (interactive "P\ncMark: ")
12039   (gnus-set-global-variables)
12040   (setq score (if score
12041                   (prefix-numeric-value score)
12042                 (or gnus-summary-default-score 0)))
12043   (save-excursion
12044     (set-buffer gnus-summary-buffer)
12045     (goto-char (point-min))
12046     (while (and (progn
12047                   (if (> (gnus-summary-article-score) score)
12048                       (gnus-summary-mark-article nil mark))
12049                   t)
12050                 (gnus-summary-find-next)))))
12051
12052 ;; Suggested by Daniel Quinlan <quinlan@best.com>.
12053 (defalias 'gnus-summary-show-all-expunged 'gnus-summary-limit-include-expunged)
12054 (defun gnus-summary-limit-include-expunged ()
12055   "Display all the hidden articles that were expunged for low scores."
12056   (interactive)
12057   (gnus-set-global-variables)
12058   (let ((buffer-read-only nil))
12059     (let ((scored gnus-newsgroup-scored)
12060           headers h)
12061       (while scored
12062         (or (gnus-summary-goto-subject (caar scored))
12063             (and (setq h (gnus-summary-article-header (caar scored)))
12064                  (< (cdar scored) gnus-summary-expunge-below)
12065                  (setq headers (cons h headers))))
12066         (setq scored (cdr scored)))
12067       (or headers (error "No expunged articles hidden."))
12068       (goto-char (point-min))
12069       (gnus-summary-prepare-unthreaded (nreverse headers)))
12070     (goto-char (point-min))
12071     (gnus-summary-position-point)))
12072
12073 (defun gnus-summary-catchup (&optional all quietly to-here not-mark)
12074   "Mark all articles not marked as unread in this newsgroup as read.
12075 If prefix argument ALL is non-nil, all articles are marked as read.
12076 If QUIETLY is non-nil, no questions will be asked.
12077 If TO-HERE is non-nil, it should be a point in the buffer.  All
12078 articles before this point will be marked as read.
12079 The number of articles marked as read is returned."
12080   (interactive "P")
12081   (gnus-set-global-variables)
12082   (prog1
12083       (if (or quietly
12084               (not gnus-interactive-catchup) ;Without confirmation?
12085               gnus-expert-user
12086               (gnus-y-or-n-p
12087                (if all
12088                    "Mark absolutely all articles as read? "
12089                  "Mark all unread articles as read? ")))
12090           (if (and not-mark
12091                    (not gnus-newsgroup-adaptive)
12092                    (not gnus-newsgroup-auto-expire))
12093               (progn
12094                 (when all
12095                   (setq gnus-newsgroup-marked nil
12096                         gnus-newsgroup-dormant nil))
12097                 (setq gnus-newsgroup-unreads nil))
12098             ;; We actually mark all articles as canceled, which we
12099             ;; have to do when using auto-expiry or adaptive scoring.
12100             (gnus-summary-show-all-threads)
12101             (if (gnus-summary-first-subject (not all))
12102                 (while (and
12103                         (if to-here (< (point) to-here) t)
12104                         (gnus-summary-mark-article-as-read gnus-catchup-mark)
12105                         (gnus-summary-find-next (not all)))))
12106             (unless to-here
12107               (setq gnus-newsgroup-unreads nil))
12108             (gnus-set-mode-line 'summary)))
12109     (let ((method (gnus-find-method-for-group gnus-newsgroup-name)))
12110       (if (and (not to-here) (eq 'nnvirtual (car method)))
12111           (nnvirtual-catchup-group
12112            (gnus-group-real-name gnus-newsgroup-name) (nth 1 method) all)))
12113     (gnus-summary-position-point)))
12114
12115 (defun gnus-summary-catchup-to-here (&optional all)
12116   "Mark all unticked articles before the current one as read.
12117 If ALL is non-nil, also mark ticked and dormant articles as read."
12118   (interactive "P")
12119   (gnus-set-global-variables)
12120   (save-excursion
12121     (let ((beg (point)))
12122       ;; We check that there are unread articles.
12123       (when (or all (gnus-summary-find-prev))
12124         (gnus-summary-catchup all t beg))))
12125   (gnus-summary-position-point))
12126
12127 (defun gnus-summary-catchup-all (&optional quietly)
12128   "Mark all articles in this newsgroup as read."
12129   (interactive "P")
12130   (gnus-set-global-variables)
12131   (gnus-summary-catchup t quietly))
12132
12133 (defun gnus-summary-catchup-and-exit (&optional all quietly)
12134   "Mark all articles not marked as unread in this newsgroup as read, then exit.
12135 If prefix argument ALL is non-nil, all articles are marked as read."
12136   (interactive "P")
12137   (gnus-set-global-variables)
12138   (gnus-summary-catchup all quietly nil 'fast)
12139   ;; Select next newsgroup or exit.
12140   (if (eq gnus-auto-select-next 'quietly)
12141       (gnus-summary-next-group nil)
12142     (gnus-summary-exit)))
12143
12144 (defun gnus-summary-catchup-all-and-exit (&optional quietly)
12145   "Mark all articles in this newsgroup as read, and then exit."
12146   (interactive "P")
12147   (gnus-set-global-variables)
12148   (gnus-summary-catchup-and-exit t quietly))
12149
12150 ;; Suggested by "Arne Eofsson" <arne@hodgkin.mbi.ucla.edu>.
12151 (defun gnus-summary-catchup-and-goto-next-group (&optional all)
12152   "Mark all articles in this group as read and select the next group.
12153 If given a prefix, mark all articles, unread as well as ticked, as
12154 read."
12155   (interactive "P")
12156   (gnus-set-global-variables)
12157   (save-excursion
12158     (gnus-summary-catchup all))
12159   (gnus-summary-next-article t nil nil t))
12160
12161 ;; Thread-based commands.
12162
12163 (defun gnus-summary-articles-in-thread (&optional article)
12164   "Return a list of all articles in the current thread.
12165 If ARTICLE is non-nil, return all articles in the thread that starts
12166 with that article."
12167   (let* ((article (or article (gnus-summary-article-number)))
12168          (data (gnus-data-find-list article))
12169          (top-level (gnus-data-level (car data)))
12170          (top-subject
12171           (cond ((null gnus-thread-operation-ignore-subject)
12172                  (gnus-simplify-subject-re
12173                   (mail-header-subject (gnus-data-header (car data)))))
12174                 ((eq gnus-thread-operation-ignore-subject 'fuzzy)
12175                  (gnus-simplify-subject-fuzzy
12176                   (mail-header-subject (gnus-data-header (car data)))))
12177                 (t nil)))
12178          articles)
12179     (if (not data)
12180         ()                              ; This article doesn't exist.
12181       (while data
12182         (and (or (not top-subject)
12183                  (string= top-subject
12184                           (if (eq gnus-thread-operation-ignore-subject 'fuzzy)
12185                               (gnus-simplify-subject-fuzzy
12186                                (mail-header-subject
12187                                 (gnus-data-header (car data))))
12188                             (gnus-simplify-subject-re
12189                              (mail-header-subject
12190                               (gnus-data-header (car data)))))))
12191              (setq articles (cons (gnus-data-number (car data)) articles)))
12192         (if (and (setq data (cdr data))
12193                  (> (gnus-data-level (car data)) top-level))
12194             ()
12195           (setq data nil)))
12196       ;; Return the list of articles.
12197       (nreverse articles))))
12198
12199 (defun gnus-summary-rethread-current ()
12200   "Rethread the thread the current article is part of."
12201   (interactive)
12202   (gnus-set-global-variables)
12203   (let* ((gnus-show-threads t)
12204          (article (gnus-summary-article-number))
12205          (id (mail-header-id (gnus-summary-article-header)))
12206          (gnus-newsgroup-threads (list (gnus-id-to-thread (gnus-root-id id)))))
12207     (unless id
12208       (error "No article on the current line"))
12209     (gnus-rebuild-thread id)
12210     (gnus-summary-goto-subject article)))
12211
12212 (defun gnus-summary-reparent-thread ()
12213   "Make current article child of the marked (or previous) article.
12214
12215 Note that the re-threading will only work if `gnus-thread-ignore-subject'
12216 is non-nil or the Subject: of both articles are the same."
12217   (interactive)
12218   (or (not (gnus-group-read-only-p))
12219       (error "The current newsgroup does not support article editing."))
12220   (or (<= (length gnus-newsgroup-processable) 1)
12221       (error "No more than one article may be marked."))
12222   (save-window-excursion
12223     (let ((gnus-article-buffer " *reparent*")
12224           (current-article (gnus-summary-article-number))
12225           ; first grab the marked article, otherwise one line up.
12226           (parent-article (if (not (null gnus-newsgroup-processable))
12227                               (car gnus-newsgroup-processable)
12228                             (save-excursion
12229                               (if (eq (forward-line -1) 0)
12230                                   (gnus-summary-article-number)
12231                                 (error "Beginning of summary buffer."))))))
12232       (or (not (eq current-article parent-article))
12233           (error "An article may not be self-referential."))
12234       (let ((message-id (mail-header-id 
12235                          (gnus-summary-article-header parent-article))))
12236         (or (and message-id (not (equal message-id "")))
12237             (error "No message-id in desired parent."))
12238         (gnus-summary-select-article t t nil current-article)
12239         (set-buffer gnus-article-buffer)
12240         (setq buffer-read-only nil)
12241         (let ((buf (buffer-substring-no-properties (point-min) (point-max))))
12242           (erase-buffer)
12243           (insert buf))
12244         (goto-char (point-min))
12245         (if (search-forward-regexp "^References: " nil t)
12246             (insert message-id " " )
12247           (insert "References: " message-id "\n"))
12248         (or (gnus-request-replace-article current-article
12249                                           (car gnus-article-current)
12250                                           gnus-article-buffer)
12251             (error "Couldn't replace article."))
12252         (set-buffer gnus-summary-buffer)
12253         (gnus-summary-unmark-all-processable)
12254         (gnus-summary-rethread-current)
12255         (message "Article %d is now the child of article %d."
12256                  current-article parent-article)))))
12257
12258 (defun gnus-summary-toggle-threads (&optional arg)
12259   "Toggle showing conversation threads.
12260 If ARG is positive number, turn showing conversation threads on."
12261   (interactive "P")
12262   (gnus-set-global-variables)
12263   (let ((current (or (gnus-summary-article-number) gnus-newsgroup-end)))
12264     (setq gnus-show-threads
12265           (if (null arg) (not gnus-show-threads)
12266             (> (prefix-numeric-value arg) 0)))
12267     (gnus-summary-prepare)
12268     (gnus-summary-goto-subject current)
12269     (gnus-summary-position-point)))
12270
12271 (defun gnus-summary-show-all-threads ()
12272   "Show all threads."
12273   (interactive)
12274   (gnus-set-global-variables)
12275   (save-excursion
12276     (let ((buffer-read-only nil))
12277       (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)))
12278   (gnus-summary-position-point))
12279
12280 (defun gnus-summary-show-thread ()
12281   "Show thread subtrees.
12282 Returns nil if no thread was there to be shown."
12283   (interactive)
12284   (gnus-set-global-variables)
12285   (let ((buffer-read-only nil)
12286         (orig (point))
12287         ;; first goto end then to beg, to have point at beg after let
12288         (end (progn (end-of-line) (point)))
12289         (beg (progn (beginning-of-line) (point))))
12290     (prog1
12291         ;; Any hidden lines here?
12292         (search-forward "\r" end t)
12293       (subst-char-in-region beg end ?\^M ?\n t)
12294       (goto-char orig)
12295       (gnus-summary-position-point))))
12296
12297 (defun gnus-summary-hide-all-threads ()
12298   "Hide all thread subtrees."
12299   (interactive)
12300   (gnus-set-global-variables)
12301   (save-excursion
12302     (goto-char (point-min))
12303     (gnus-summary-hide-thread)
12304     (while (zerop (gnus-summary-next-thread 1 t))
12305       (gnus-summary-hide-thread)))
12306   (gnus-summary-position-point))
12307
12308 (defun gnus-summary-hide-thread ()
12309   "Hide thread subtrees.
12310 Returns nil if no threads were there to be hidden."
12311   (interactive)
12312   (gnus-set-global-variables)
12313   (let ((buffer-read-only nil)
12314         (start (point))
12315         (article (gnus-summary-article-number))
12316         end)
12317     (goto-char start)
12318     ;; Go forward until either the buffer ends or the subthread
12319     ;; ends.
12320     (when (and (not (eobp))
12321                (or (zerop (gnus-summary-next-thread 1 t))
12322                    (goto-char (point-max))))
12323       (setq end (point))
12324       (prog1
12325           (if (and (> (point) start)
12326                    (search-backward "\n" start t))
12327               (progn
12328                 (subst-char-in-region start (point) ?\n ?\^M)
12329                 (gnus-summary-goto-subject article))
12330             (goto-char start)
12331             nil)
12332         ;;(gnus-summary-position-point)
12333         ))))
12334
12335 (defun gnus-summary-go-to-next-thread (&optional previous)
12336   "Go to the same level (or less) next thread.
12337 If PREVIOUS is non-nil, go to previous thread instead.
12338 Return the article number moved to, or nil if moving was impossible."
12339   (let* ((level (gnus-summary-thread-level))
12340          (article (gnus-summary-article-number))
12341          (data (cdr (gnus-data-find-list article (gnus-data-list previous))))
12342          oart)
12343     (while data
12344       (if (<= (gnus-data-level (car data)) level)
12345           (setq oart (gnus-data-number (car data))
12346                 data nil)
12347         (setq data (cdr data))))
12348     (and oart
12349          (gnus-summary-goto-subject oart))))
12350
12351 (defun gnus-summary-next-thread (n &optional silent)
12352   "Go to the same level next N'th thread.
12353 If N is negative, search backward instead.
12354 Returns the difference between N and the number of skips actually
12355 done.
12356
12357 If SILENT, don't output messages."
12358   (interactive "p")
12359   (gnus-set-global-variables)
12360   (let ((backward (< n 0))
12361         (n (abs n))
12362         old dum int)
12363     (while (and (> n 0)
12364                 (setq old (save-excursion 
12365                             (forward-line 1) 
12366                             (setq int (gnus-summary-article-intangible-p))
12367                             (point)))
12368                 (or int 
12369                     (gnus-summary-go-to-next-thread backward)))
12370       (when (and (eq gnus-summary-make-false-root 'dummy)
12371                  (setq dum (text-property-not-all
12372                             old (point) 'gnus-intangible nil)))
12373         (goto-char dum))
12374       (decf n))
12375     (unless silent 
12376       (gnus-summary-position-point))
12377     (when (and (not silent) (/= 0 n))
12378       (gnus-message 7 "No more threads"))
12379     n))
12380
12381 (defun gnus-summary-prev-thread (n)
12382   "Go to the same level previous N'th thread.
12383 Returns the difference between N and the number of skips actually
12384 done."
12385   (interactive "p")
12386   (gnus-set-global-variables)
12387   (gnus-summary-next-thread (- n)))
12388
12389 (defun gnus-summary-go-down-thread ()
12390   "Go down one level in the current thread."
12391   (let ((children (gnus-summary-article-children)))
12392     (and children
12393          (gnus-summary-goto-subject (car children)))))
12394
12395 (defun gnus-summary-go-up-thread ()
12396   "Go up one level in the current thread."
12397   (let ((parent (gnus-summary-article-parent)))
12398     (and parent
12399          (gnus-summary-goto-subject parent))))
12400
12401 (defun gnus-summary-down-thread (n)
12402   "Go down thread N steps.
12403 If N is negative, go up instead.
12404 Returns the difference between N and how many steps down that were
12405 taken."
12406   (interactive "p")
12407   (gnus-set-global-variables)
12408   (let ((up (< n 0))
12409         (n (abs n)))
12410     (while (and (> n 0)
12411                 (if up (gnus-summary-go-up-thread)
12412                   (gnus-summary-go-down-thread)))
12413       (setq n (1- n)))
12414     (gnus-summary-position-point)
12415     (if (/= 0 n) (gnus-message 7 "Can't go further"))
12416     n))
12417
12418 (defun gnus-summary-up-thread (n)
12419   "Go up thread N steps.
12420 If N is negative, go up instead.
12421 Returns the difference between N and how many steps down that were
12422 taken."
12423   (interactive "p")
12424   (gnus-set-global-variables)
12425   (gnus-summary-down-thread (- n)))
12426
12427 (defun gnus-summary-top-thread ()
12428   "Go to the top of the thread."
12429   (interactive)
12430   (gnus-set-global-variables)
12431   (while (gnus-summary-go-up-thread))
12432   (gnus-summary-article-number))
12433
12434 (defun gnus-summary-kill-thread (&optional unmark)
12435   "Mark articles under current thread as read.
12436 If the prefix argument is positive, remove any kinds of marks.
12437 If the prefix argument is negative, tick articles instead."
12438   (interactive "P")
12439   (gnus-set-global-variables)
12440   (if unmark
12441       (setq unmark (prefix-numeric-value unmark)))
12442   (let ((articles (gnus-summary-articles-in-thread)))
12443     (save-excursion
12444       ;; Expand the thread.
12445       (gnus-summary-show-thread)
12446       ;; Mark all the articles.
12447       (while articles
12448         (gnus-summary-goto-subject (car articles))
12449         (cond ((null unmark)
12450                (gnus-summary-mark-article-as-read gnus-killed-mark))
12451               ((> unmark 0)
12452                (gnus-summary-mark-article-as-unread gnus-unread-mark))
12453               (t
12454                (gnus-summary-mark-article-as-unread gnus-ticked-mark)))
12455         (setq articles (cdr articles))))
12456     ;; Hide killed subtrees.
12457     (and (null unmark)
12458          gnus-thread-hide-killed
12459          (gnus-summary-hide-thread))
12460     ;; If marked as read, go to next unread subject.
12461     (if (null unmark)
12462         ;; Go to next unread subject.
12463         (gnus-summary-next-subject 1 t)))
12464   (gnus-set-mode-line 'summary))
12465
12466 ;; Summary sorting commands
12467
12468 (defun gnus-summary-sort-by-number (&optional reverse)
12469   "Sort summary buffer by article number.
12470 Argument REVERSE means reverse order."
12471   (interactive "P")
12472   (gnus-summary-sort 'number reverse))
12473
12474 (defun gnus-summary-sort-by-author (&optional reverse)
12475   "Sort summary buffer by author name alphabetically.
12476 If case-fold-search is non-nil, case of letters is ignored.
12477 Argument REVERSE means reverse order."
12478   (interactive "P")
12479   (gnus-summary-sort 'author reverse))
12480
12481 (defun gnus-summary-sort-by-subject (&optional reverse)
12482   "Sort summary buffer by subject alphabetically. `Re:'s are ignored.
12483 If case-fold-search is non-nil, case of letters is ignored.
12484 Argument REVERSE means reverse order."
12485   (interactive "P")
12486   (gnus-summary-sort 'subject reverse))
12487
12488 (defun gnus-summary-sort-by-date (&optional reverse)
12489   "Sort summary buffer by date.
12490 Argument REVERSE means reverse order."
12491   (interactive "P")
12492   (gnus-summary-sort 'date reverse))
12493
12494 (defun gnus-summary-sort-by-score (&optional reverse)
12495   "Sort summary buffer by score.
12496 Argument REVERSE means reverse order."
12497   (interactive "P")
12498   (gnus-summary-sort 'score reverse))
12499
12500 (defun gnus-summary-sort (predicate reverse)
12501   "Sort summary buffer by PREDICATE.  REVERSE means reverse order."
12502   (gnus-set-global-variables)
12503   (let* ((thread (intern (format "gnus-thread-sort-by-%s" predicate)))
12504          (article (intern (format "gnus-article-sort-by-%s" predicate)))
12505          (gnus-thread-sort-functions
12506           (list
12507            (if (not reverse)
12508                thread
12509              `(lambda (t1 t2)
12510                 (,thread t2 t1)))))
12511          (gnus-article-sort-functions
12512           (list
12513            (if (not reverse)
12514                article
12515              `(lambda (t1 t2)
12516                 (,article t2 t1)))))
12517          (buffer-read-only)
12518          (gnus-summary-prepare-hook nil))
12519     ;; We do the sorting by regenerating the threads.
12520     (gnus-summary-prepare)
12521     ;; Hide subthreads if needed.
12522     (when (and gnus-show-threads gnus-thread-hide-subtree)
12523       (gnus-summary-hide-all-threads)))
12524   ;; If in async mode, we send some info to the backend.
12525   (when gnus-newsgroup-async
12526     (gnus-request-asynchronous
12527      gnus-newsgroup-name gnus-newsgroup-data)))
12528
12529 (defun gnus-sortable-date (date)
12530   "Make sortable string by string-lessp from DATE.
12531 Timezone package is used."
12532   (let* ((date (timezone-fix-time date nil nil)) ;[Y M D H M S]
12533          (year (aref date 0))
12534          (month (aref date 1))
12535          (day (aref date 2)))
12536     (timezone-make-sortable-date
12537      year month day
12538      (timezone-make-time-string
12539       (aref date 3) (aref date 4) (aref date 5)))))
12540
12541 ;; Summary saving commands.
12542
12543 (defun gnus-summary-save-article (&optional n not-saved)
12544   "Save the current article using the default saver function.
12545 If N is a positive number, save the N next articles.
12546 If N is a negative number, save the N previous articles.
12547 If N is nil and any articles have been marked with the process mark,
12548 save those articles instead.
12549 The variable `gnus-default-article-saver' specifies the saver function."
12550   (interactive "P")
12551   (gnus-set-global-variables)
12552   (let ((articles (gnus-summary-work-articles n))
12553         file header article)
12554     (while articles
12555       (setq header (gnus-summary-article-header
12556                     (setq article (pop articles))))
12557       (if (not (vectorp header))
12558           ;; This is a pseudo-article.
12559           (if (assq 'name header)
12560               (gnus-copy-file (cdr (assq 'name header)))
12561             (gnus-message 1 "Article %d is unsaveable" article))
12562         ;; This is a real article.
12563         (save-window-excursion
12564           (gnus-summary-select-article t nil nil article))
12565         (unless gnus-save-all-headers
12566           ;; Remove headers accoring to `gnus-saved-headers'.
12567           (let ((gnus-visible-headers
12568                  (or gnus-saved-headers gnus-visible-headers)))
12569             (gnus-article-hide-headers nil t)))
12570         ;; Remove any X-Gnus lines.
12571         (save-excursion
12572           (set-buffer gnus-article-buffer)
12573           (save-restriction
12574             (let ((buffer-read-only nil))
12575               (nnheader-narrow-to-headers)
12576               (while (re-search-forward "^X-Gnus" nil t)
12577                 (gnus-delete-line)))))
12578         (save-window-excursion
12579           (if (not gnus-default-article-saver)
12580               (error "No default saver is defined.")
12581             (setq file (funcall
12582                         gnus-default-article-saver
12583                         (cond
12584                          ((not gnus-prompt-before-saving)
12585                           'default)
12586                          ((eq gnus-prompt-before-saving 'always)
12587                           nil)
12588                          (t file))))))
12589         (gnus-summary-remove-process-mark article)
12590         (unless not-saved
12591           (gnus-summary-set-saved-mark article))))
12592     (gnus-summary-position-point)
12593     n))
12594
12595 (defun gnus-summary-pipe-output (&optional arg)
12596   "Pipe the current article to a subprocess.
12597 If N is a positive number, pipe the N next articles.
12598 If N is a negative number, pipe the N previous articles.
12599 If N is nil and any articles have been marked with the process mark,
12600 pipe those articles instead."
12601   (interactive "P")
12602   (gnus-set-global-variables)
12603   (let ((gnus-default-article-saver 'gnus-summary-save-in-pipe))
12604     (gnus-summary-save-article arg t))
12605   (gnus-configure-windows 'pipe))
12606
12607 (defun gnus-summary-save-article-mail (&optional arg)
12608   "Append the current article to an mail file.
12609 If N is a positive number, save the N next articles.
12610 If N is a negative number, save the N previous articles.
12611 If N is nil and any articles have been marked with the process mark,
12612 save those articles instead."
12613   (interactive "P")
12614   (gnus-set-global-variables)
12615   (let ((gnus-default-article-saver 'gnus-summary-save-in-mail))
12616     (gnus-summary-save-article arg)))
12617
12618 (defun gnus-summary-save-article-rmail (&optional arg)
12619   "Append the current article to an rmail file.
12620 If N is a positive number, save the N next articles.
12621 If N is a negative number, save the N previous articles.
12622 If N is nil and any articles have been marked with the process mark,
12623 save those articles instead."
12624   (interactive "P")
12625   (gnus-set-global-variables)
12626   (let ((gnus-default-article-saver 'gnus-summary-save-in-rmail))
12627     (gnus-summary-save-article arg)))
12628
12629 (defun gnus-summary-save-article-file (&optional arg)
12630   "Append the current article to a file.
12631 If N is a positive number, save the N next articles.
12632 If N is a negative number, save the N previous articles.
12633 If N is nil and any articles have been marked with the process mark,
12634 save those articles instead."
12635   (interactive "P")
12636   (gnus-set-global-variables)
12637   (let ((gnus-default-article-saver 'gnus-summary-save-in-file))
12638     (gnus-summary-save-article arg)))
12639
12640 (defun gnus-summary-save-article-body-file (&optional arg)
12641   "Append the current article body to a file.
12642 If N is a positive number, save the N next articles.
12643 If N is a negative number, save the N previous articles.
12644 If N is nil and any articles have been marked with the process mark,
12645 save those articles instead."
12646   (interactive "P")
12647   (gnus-set-global-variables)
12648   (let ((gnus-default-article-saver 'gnus-summary-save-body-in-file))
12649     (gnus-summary-save-article arg)))
12650
12651 (defun gnus-get-split-value (methods)
12652   "Return a value based on the split METHODS."
12653   (let (split-name method result match)
12654     (when methods
12655       (save-excursion
12656         (set-buffer gnus-original-article-buffer)
12657         (save-restriction
12658           (nnheader-narrow-to-headers)
12659           (while methods
12660             (goto-char (point-min))
12661             (setq method (pop methods))
12662             (setq match (pop method))
12663             (when (cond
12664                    ((stringp match)
12665                     ;; Regular expression.
12666                     (condition-case ()
12667                         (re-search-forward match nil t)
12668                       (error nil)))
12669                    ((gnus-functionp match)
12670                     ;; Function.
12671                     (save-restriction
12672                       (widen)
12673                       (setq result (funcall match gnus-newsgroup-name))))
12674                    ((consp match)
12675                     ;; Form.
12676                     (save-restriction
12677                       (widen)
12678                       (setq result (eval match)))))
12679               (setq split-name (append (cdr methods) split-name))
12680               (cond ((stringp result)
12681                      (push result split-name))
12682                     ((consp result)
12683                      (setq split-name (append result split-name)))))))))
12684     split-name))
12685
12686 (defun gnus-read-move-group-name (prompt default articles prefix)
12687   "Read a group name."
12688   (let* ((split-name (gnus-get-split-value gnus-move-split-methods))
12689          (prom
12690           (format "Where do you want to %s %s? "
12691                   prompt
12692                   (if (> (length articles) 1)
12693                       (format "these %d articles" (length articles))
12694                     "this article")))
12695          (to-newsgroup
12696           (cond
12697            ((null split-name)
12698             (completing-read
12699              (concat prom
12700                      (if default
12701                          (format "(default %s) " default)
12702                        ""))
12703              gnus-active-hashtb nil nil prefix))
12704            ((= 1 (length split-name))
12705             (completing-read prom gnus-active-hashtb
12706                              nil nil (cons (car split-name) 0)))
12707            (t
12708             (completing-read
12709              prom (mapcar (lambda (el) (list el)) (nreverse split-name)))))))
12710
12711     (when to-newsgroup
12712       (if (or (string= to-newsgroup "")
12713               (string= to-newsgroup prefix))
12714           (setq to-newsgroup (or default "")))
12715       (or (gnus-active to-newsgroup)
12716           (gnus-activate-group to-newsgroup)
12717           (error "No such group: %s" to-newsgroup)))
12718     to-newsgroup))
12719
12720 (defun gnus-read-save-file-name (prompt default-name)
12721   (let* ((split-name (gnus-get-split-value gnus-split-methods))
12722          (file
12723           ;; Let the split methods have their say.
12724           (cond
12725            ;; No split name was found.
12726            ((null split-name)
12727             (read-file-name
12728              (concat prompt " (default "
12729                      (file-name-nondirectory default-name) ") ")
12730              (file-name-directory default-name)
12731              default-name))
12732            ;; A single split name was found
12733            ((= 1 (length split-name))
12734             (read-file-name
12735              (concat prompt " (default " (car split-name) ") ")
12736              gnus-article-save-directory
12737              (concat gnus-article-save-directory (car split-name))))
12738            ;; A list of splits was found.
12739            (t
12740             (setq split-name (mapcar (lambda (el) (list el))
12741                                      (nreverse split-name)))
12742             (let ((result (completing-read
12743                            (concat prompt " ") split-name nil nil)))
12744               (concat gnus-article-save-directory
12745                       (if (string= result "")
12746                           (caar split-name)
12747                         result)))))))
12748     ;; If we have read a directory, we append the default file name.
12749     (when (file-directory-p file)
12750       (setq file (concat (file-name-as-directory file)
12751                          (file-name-nondirectory default-name))))
12752     ;; Possibly translate some charaters.
12753     (nnheader-translate-file-chars file)))
12754
12755 (defun gnus-article-archive-name (group)
12756   "Return the first instance of an \"Archive-name\" in the current buffer."
12757   (let ((case-fold-search t))
12758     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
12759       (match-string 1))))
12760
12761 (defun gnus-summary-save-in-rmail (&optional filename)
12762   "Append this article to Rmail file.
12763 Optional argument FILENAME specifies file name.
12764 Directory to save to is default to `gnus-article-save-directory' which
12765 is initialized from the SAVEDIR environment variable."
12766   (interactive)
12767   (gnus-set-global-variables)
12768   (let ((default-name
12769           (funcall gnus-rmail-save-name gnus-newsgroup-name
12770                    gnus-current-headers gnus-newsgroup-last-rmail)))
12771     (setq filename
12772           (cond ((eq filename 'default)
12773                  default-name)
12774                 (filename filename)
12775                 (t (gnus-read-save-file-name
12776                     "Save in rmail file:" default-name))))
12777     (gnus-make-directory (file-name-directory filename))
12778     (gnus-eval-in-buffer-window
12779      gnus-original-article-buffer
12780      (save-excursion
12781        (save-restriction
12782          (widen)
12783          (gnus-output-to-rmail filename))))
12784     ;; Remember the directory name to save articles
12785     (setq gnus-newsgroup-last-rmail filename)))
12786
12787 (defun gnus-summary-save-in-mail (&optional filename)
12788   "Append this article to Unix mail file.
12789 Optional argument FILENAME specifies file name.
12790 Directory to save to is default to `gnus-article-save-directory' which
12791 is initialized from the SAVEDIR environment variable."
12792   (interactive)
12793   (gnus-set-global-variables)
12794   (let ((default-name
12795           (funcall gnus-mail-save-name gnus-newsgroup-name
12796                    gnus-current-headers gnus-newsgroup-last-mail)))
12797     (setq filename
12798           (cond ((eq filename 'default)
12799                  default-name)
12800                 (filename filename)
12801                 (t (gnus-read-save-file-name
12802                     "Save in Unix mail file:" default-name))))
12803     (setq filename
12804           (expand-file-name filename
12805                             (and default-name
12806                                  (file-name-directory default-name))))
12807     (gnus-make-directory (file-name-directory filename))
12808     (gnus-eval-in-buffer-window
12809      gnus-original-article-buffer
12810      (save-excursion
12811        (save-restriction
12812          (widen)
12813          (if (and (file-readable-p filename) (mail-file-babyl-p filename))
12814              (gnus-output-to-rmail filename)
12815            (let ((mail-use-rfc822 t))
12816              (rmail-output filename 1 t t))))))
12817     ;; Remember the directory name to save articles.
12818     (setq gnus-newsgroup-last-mail filename)))
12819
12820 (defun gnus-summary-save-in-file (&optional filename)
12821   "Append this article to file.
12822 Optional argument FILENAME specifies file name.
12823 Directory to save to is default to `gnus-article-save-directory' which
12824 is initialized from the SAVEDIR environment variable."
12825   (interactive)
12826   (gnus-set-global-variables)
12827   (let ((default-name
12828           (funcall gnus-file-save-name gnus-newsgroup-name
12829                    gnus-current-headers gnus-newsgroup-last-file)))
12830     (setq filename
12831           (cond ((eq filename 'default)
12832                  default-name)
12833                 (filename filename)
12834                 (t (gnus-read-save-file-name
12835                     "Save in file:" default-name))))
12836     (gnus-make-directory (file-name-directory filename))
12837     (gnus-eval-in-buffer-window
12838      gnus-original-article-buffer
12839      (save-excursion
12840        (save-restriction
12841          (widen)
12842          (gnus-output-to-file filename))))
12843     ;; Remember the directory name to save articles.
12844     (setq gnus-newsgroup-last-file filename)))
12845
12846 (defun gnus-summary-save-body-in-file (&optional filename)
12847   "Append this article body to a file.
12848 Optional argument FILENAME specifies file name.
12849 The directory to save in defaults to `gnus-article-save-directory' which
12850 is initialized from the SAVEDIR environment variable."
12851   (interactive)
12852   (gnus-set-global-variables)
12853   (let ((default-name
12854           (funcall gnus-file-save-name gnus-newsgroup-name
12855                    gnus-current-headers gnus-newsgroup-last-file)))
12856     (setq filename
12857           (cond ((eq filename 'default)
12858                  default-name)
12859                 (filename filename)
12860                 (t (gnus-read-save-file-name
12861                     "Save body in file:" default-name))))
12862     (gnus-make-directory (file-name-directory filename))
12863     (gnus-eval-in-buffer-window
12864      gnus-article-buffer
12865      (save-excursion
12866        (save-restriction
12867          (widen)
12868          (goto-char (point-min))
12869          (and (search-forward "\n\n" nil t)
12870               (narrow-to-region (point) (point-max)))
12871          (gnus-output-to-file filename))))
12872     ;; Remember the directory name to save articles.
12873     (setq gnus-newsgroup-last-file filename)))
12874
12875 (defun gnus-summary-save-in-pipe (&optional command)
12876   "Pipe this article to subprocess."
12877   (interactive)
12878   (gnus-set-global-variables)
12879   (setq command
12880         (cond ((eq command 'default)
12881                gnus-last-shell-command)
12882               (command command)
12883               (t (read-string "Shell command on article: "
12884                               gnus-last-shell-command))))
12885   (if (string-equal command "")
12886       (setq command gnus-last-shell-command))
12887   (gnus-eval-in-buffer-window
12888    gnus-article-buffer
12889    (save-restriction
12890      (widen)
12891      (shell-command-on-region (point-min) (point-max) command nil)))
12892   (setq gnus-last-shell-command command))
12893
12894 ;; Summary extract commands
12895
12896 (defun gnus-summary-insert-pseudos (pslist &optional not-view)
12897   (let ((buffer-read-only nil)
12898         (article (gnus-summary-article-number))
12899         after-article b e)
12900     (or (gnus-summary-goto-subject article)
12901         (error (format "No such article: %d" article)))
12902     (gnus-summary-position-point)
12903     ;; If all commands are to be bunched up on one line, we collect
12904     ;; them here.
12905     (if gnus-view-pseudos-separately
12906         ()
12907       (let ((ps (setq pslist (sort pslist 'gnus-pseudos<)))
12908             files action)
12909         (while ps
12910           (setq action (cdr (assq 'action (car ps))))
12911           (setq files (list (cdr (assq 'name (car ps)))))
12912           (while (and ps (cdr ps)
12913                       (string= (or action "1")
12914                                (or (cdr (assq 'action (cadr ps))) "2")))
12915             (setq files (cons (cdr (assq 'name (cadr ps))) files))
12916             (setcdr ps (cddr ps)))
12917           (if (not files)
12918               ()
12919             (if (not (string-match "%s" action))
12920                 (setq files (cons " " files)))
12921             (setq files (cons " " files))
12922             (and (assq 'execute (car ps))
12923                  (setcdr (assq 'execute (car ps))
12924                          (funcall (if (string-match "%s" action)
12925                                       'format 'concat)
12926                                   action
12927                                   (mapconcat (lambda (f) f) files " ")))))
12928           (setq ps (cdr ps)))))
12929     (if (and gnus-view-pseudos (not not-view))
12930         (while pslist
12931           (and (assq 'execute (car pslist))
12932                (gnus-execute-command (cdr (assq 'execute (car pslist)))
12933                                      (eq gnus-view-pseudos 'not-confirm)))
12934           (setq pslist (cdr pslist)))
12935       (save-excursion
12936         (while pslist
12937           (setq after-article (or (cdr (assq 'article (car pslist)))
12938                                   (gnus-summary-article-number)))
12939           (gnus-summary-goto-subject after-article)
12940           (forward-line 1)
12941           (setq b (point))
12942           (insert "          " (file-name-nondirectory
12943                                 (cdr (assq 'name (car pslist))))
12944                   ": " (or (cdr (assq 'execute (car pslist))) "") "\n")
12945           (setq e (point))
12946           (forward-line -1)             ; back to `b'
12947           (add-text-properties
12948            b e (list 'gnus-number gnus-reffed-article-number
12949                      gnus-mouse-face-prop gnus-mouse-face))
12950           (gnus-data-enter
12951            after-article gnus-reffed-article-number
12952            gnus-unread-mark b (car pslist) 0 (- e b))
12953           (push gnus-reffed-article-number gnus-newsgroup-unreads)
12954           (setq gnus-reffed-article-number (1- gnus-reffed-article-number))
12955           (setq pslist (cdr pslist)))))))
12956
12957 (defun gnus-pseudos< (p1 p2)
12958   (let ((c1 (cdr (assq 'action p1)))
12959         (c2 (cdr (assq 'action p2))))
12960     (and c1 c2 (string< c1 c2))))
12961
12962 (defun gnus-request-pseudo-article (props)
12963   (cond ((assq 'execute props)
12964          (gnus-execute-command (cdr (assq 'execute props)))))
12965   (let ((gnus-current-article (gnus-summary-article-number)))
12966     (run-hooks 'gnus-mark-article-hook)))
12967
12968 (defun gnus-execute-command (command &optional automatic)
12969   (save-excursion
12970     (gnus-article-setup-buffer)
12971     (set-buffer gnus-article-buffer)
12972     (let ((command (if automatic command (read-string "Command: " command)))
12973           (buffer-read-only nil))
12974       (erase-buffer)
12975       (insert "$ " command "\n\n")
12976       (if gnus-view-pseudo-asynchronously
12977           (start-process "gnus-execute" nil "sh" "-c" command)
12978         (call-process "sh" nil t nil "-c" command)))))
12979
12980 (defun gnus-copy-file (file &optional to)
12981   "Copy FILE to TO."
12982   (interactive
12983    (list (read-file-name "Copy file: " default-directory)
12984          (read-file-name "Copy file to: " default-directory)))
12985   (gnus-set-global-variables)
12986   (or to (setq to (read-file-name "Copy file to: " default-directory)))
12987   (and (file-directory-p to)
12988        (setq to (concat (file-name-as-directory to)
12989                         (file-name-nondirectory file))))
12990   (copy-file file to))
12991
12992 ;; Summary kill commands.
12993
12994 (defun gnus-summary-edit-global-kill (article)
12995   "Edit the \"global\" kill file."
12996   (interactive (list (gnus-summary-article-number)))
12997   (gnus-set-global-variables)
12998   (gnus-group-edit-global-kill article))
12999
13000 (defun gnus-summary-edit-local-kill ()
13001   "Edit a local kill file applied to the current newsgroup."
13002   (interactive)
13003   (gnus-set-global-variables)
13004   (setq gnus-current-headers (gnus-summary-article-header))
13005   (gnus-set-global-variables)
13006   (gnus-group-edit-local-kill
13007    (gnus-summary-article-number) gnus-newsgroup-name))
13008
13009 \f
13010 ;;;
13011 ;;; Gnus article mode
13012 ;;;
13013
13014 (put 'gnus-article-mode 'mode-class 'special)
13015
13016 (if gnus-article-mode-map
13017     nil
13018   (setq gnus-article-mode-map (make-keymap))
13019   (suppress-keymap gnus-article-mode-map)
13020
13021   (gnus-define-keys gnus-article-mode-map
13022     " " gnus-article-goto-next-page
13023     "\177" gnus-article-goto-prev-page
13024     [delete] gnus-article-goto-prev-page
13025     "\C-c^" gnus-article-refer-article
13026     "h" gnus-article-show-summary
13027     "s" gnus-article-show-summary
13028     "\C-c\C-m" gnus-article-mail
13029     "?" gnus-article-describe-briefly
13030     gnus-mouse-2 gnus-article-push-button
13031     "\r" gnus-article-press-button
13032     "\t" gnus-article-next-button
13033     "\M-\t" gnus-article-prev-button
13034     "\C-c\C-b" gnus-bug)
13035
13036   (substitute-key-definition
13037    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
13038
13039 (defun gnus-article-mode ()
13040   "Major mode for displaying an article.
13041
13042 All normal editing commands are switched off.
13043
13044 The following commands are available:
13045
13046 \\<gnus-article-mode-map>
13047 \\[gnus-article-next-page]\t Scroll the article one page forwards
13048 \\[gnus-article-prev-page]\t Scroll the article one page backwards
13049 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
13050 \\[gnus-article-show-summary]\t Display the summary buffer
13051 \\[gnus-article-mail]\t Send a reply to the address near point
13052 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
13053 \\[gnus-info-find-node]\t Go to the Gnus info node"
13054   (interactive)
13055   (when (and menu-bar-mode
13056              (gnus-visual-p 'article-menu 'menu))
13057     (gnus-article-make-menu-bar))
13058   (kill-all-local-variables)
13059   (gnus-simplify-mode-line)
13060   (setq mode-name "Article")
13061   (setq major-mode 'gnus-article-mode)
13062   (make-local-variable 'minor-mode-alist)
13063   (or (assq 'gnus-show-mime minor-mode-alist)
13064       (setq minor-mode-alist
13065             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
13066   (use-local-map gnus-article-mode-map)
13067   (make-local-variable 'page-delimiter)
13068   (setq page-delimiter gnus-page-delimiter)
13069   (buffer-disable-undo (current-buffer))
13070   (setq buffer-read-only t)             ;Disable modification
13071   (run-hooks 'gnus-article-mode-hook))
13072
13073 (defun gnus-article-setup-buffer ()
13074   "Initialize the article buffer."
13075   (let* ((name (if gnus-single-article-buffer "*Article*"
13076                  (concat "*Article " gnus-newsgroup-name "*")))
13077          (original
13078           (progn (string-match "\\*Article" name)
13079                  (concat " *Original Article"
13080                          (substring name (match-end 0))))))
13081     (setq gnus-article-buffer name)
13082     (setq gnus-original-article-buffer original)
13083     ;; This might be a variable local to the summary buffer.
13084     (unless gnus-single-article-buffer
13085       (save-excursion
13086         (set-buffer gnus-summary-buffer)
13087         (setq gnus-article-buffer name)
13088         (setq gnus-original-article-buffer original)
13089         (gnus-set-global-variables))
13090       (make-local-variable 'gnus-summary-buffer))
13091     (if (get-buffer name)
13092         (save-excursion
13093           (set-buffer name)
13094           (buffer-disable-undo (current-buffer))
13095           (setq buffer-read-only t)
13096           (gnus-add-current-to-buffer-list)
13097           (or (eq major-mode 'gnus-article-mode)
13098               (gnus-article-mode))
13099           (current-buffer))
13100       (save-excursion
13101         (set-buffer (get-buffer-create name))
13102         (gnus-add-current-to-buffer-list)
13103         (gnus-article-mode)
13104         (current-buffer)))))
13105
13106 ;; Set article window start at LINE, where LINE is the number of lines
13107 ;; from the head of the article.
13108 (defun gnus-article-set-window-start (&optional line)
13109   (set-window-start
13110    (get-buffer-window gnus-article-buffer)
13111    (save-excursion
13112      (set-buffer gnus-article-buffer)
13113      (goto-char (point-min))
13114      (if (not line)
13115          (point-min)
13116        (gnus-message 6 "Moved to bookmark")
13117        (search-forward "\n\n" nil t)
13118        (forward-line line)
13119        (point)))))
13120
13121 (defun gnus-kill-all-overlays ()
13122   "Delete all overlays in the current buffer."
13123   (when (fboundp 'overlay-lists)
13124     (let* ((overlayss (overlay-lists))
13125            (buffer-read-only nil)
13126            (overlays (nconc (car overlayss) (cdr overlayss))))
13127       (while overlays
13128         (delete-overlay (pop overlays))))))
13129
13130 (defun gnus-request-article-this-buffer (article group)
13131   "Get an article and insert it into this buffer."
13132   (prog1
13133       (save-excursion
13134         (erase-buffer)
13135         (gnus-kill-all-overlays)
13136         (setq group (or group gnus-newsgroup-name))
13137
13138         ;; Open server if it has closed.
13139         (gnus-check-server (gnus-find-method-for-group group))
13140
13141         ;; Using `gnus-request-article' directly will insert the article into
13142         ;; `nntp-server-buffer' - so we'll save some time by not having to
13143         ;; copy it from the server buffer into the article buffer.
13144
13145         ;; We only request an article by message-id when we do not have the
13146         ;; headers for it, so we'll have to get those.
13147         (when (stringp article)
13148           (let ((gnus-override-method gnus-refer-article-method))
13149             (gnus-read-header article)))
13150
13151         ;; If the article number is negative, that means that this article
13152         ;; doesn't belong in this newsgroup (possibly), so we find its
13153         ;; message-id and request it by id instead of number.
13154         (when (and (numberp article)
13155                    gnus-summary-buffer
13156                    (buffer-name gnus-summary-buffer))
13157           (save-excursion
13158             (set-buffer gnus-summary-buffer)
13159             (let ((header (gnus-summary-article-header article)))
13160               (if (< article 0)
13161                   (cond 
13162                    ((memq article gnus-newsgroup-sparse)
13163                     ;; This is a sparse gap article.
13164                     (setq article (mail-header-id header)))
13165                    ((vectorp header)
13166                     ;; It's a real article.
13167                     (setq article (mail-header-id header)))
13168                    (t
13169                     ;; It is an extracted pseudo-article.
13170                     (setq article 'pseudo)
13171                     (gnus-request-pseudo-article header))))
13172                 
13173               (let ((method (gnus-find-method-for-group 
13174                              gnus-newsgroup-name)))
13175                 (if (not (eq (car method) 'nneething))
13176                     ()
13177                   (let ((dir (concat (file-name-as-directory (nth 1 method))
13178                                      (mail-header-subject header))))
13179                     (if (file-directory-p dir)
13180                         (progn
13181                           (setq article 'nneething)
13182                           (gnus-group-enter-directory dir)))))))))
13183
13184         (cond
13185          ;; We first check `gnus-original-article-buffer'.
13186          ((and (equal (car gnus-original-article) group)
13187                (eq (cdr gnus-original-article) article)
13188                (get-buffer gnus-original-article-buffer))
13189           (insert-buffer-substring gnus-original-article-buffer)
13190           'article)
13191          ;; Check the backlog.
13192          ((and gnus-keep-backlog
13193                (gnus-backlog-request-article group article (current-buffer)))
13194           'article)
13195          ;; Check the cache.
13196          ((and gnus-use-cache
13197                (numberp article)
13198                (gnus-cache-request-article article group))
13199           'article)
13200          ;; Get the article and put into the article buffer.
13201          ((or (stringp article) (numberp article))
13202           (let ((gnus-override-method
13203                  (and (stringp article) gnus-refer-article-method))
13204                 (buffer-read-only nil))
13205             (erase-buffer)
13206             (gnus-kill-all-overlays)
13207             (if (gnus-request-article article group (current-buffer))
13208                 (progn
13209                   (and gnus-keep-backlog
13210                        (gnus-backlog-enter-article
13211                         group article (current-buffer)))
13212                   'article))))
13213          ;; It was a pseudo.
13214          (t article)))
13215
13216     ;; Take the article from the original article buffer
13217     ;; and place it in the buffer it's supposed to be in.
13218     (setq gnus-original-article (cons group article))
13219     (when (and (get-buffer gnus-article-buffer)
13220                (equal (buffer-name (current-buffer))
13221                       (buffer-name (get-buffer gnus-article-buffer))))
13222       (save-excursion
13223         (if (get-buffer gnus-original-article-buffer)
13224             (set-buffer (get-buffer gnus-original-article-buffer))
13225           (set-buffer (get-buffer-create gnus-original-article-buffer))
13226           (buffer-disable-undo (current-buffer))
13227           (setq major-mode 'gnus-original-article-mode)
13228           (setq buffer-read-only t)
13229           (gnus-add-current-to-buffer-list))
13230         (let (buffer-read-only)
13231           (erase-buffer)
13232           (insert-buffer-substring gnus-article-buffer))))
13233     
13234     ;; Update sparse articles.
13235     (when (memq article gnus-newsgroup-sparse)
13236       (gnus-summary-update-article article))))
13237
13238 (defun gnus-read-header (id)
13239   "Read the headers of article ID and enter them into the Gnus system."
13240   (let ((group gnus-newsgroup-name)
13241         (headers gnus-newsgroup-headers)
13242         header where)
13243     ;; First we check to see whether the header in question is already
13244     ;; fetched.
13245     (if (stringp id)
13246         ;; This is a Message-ID.
13247         (setq header (gnus-id-to-header id))
13248       ;; This is an article number.
13249       (setq header (gnus-summary-article-header id)))
13250     (if header
13251         ;; We have found the header.
13252         header
13253       ;; We have to really fetch the header to this article.
13254       (when (setq where
13255                   (if (gnus-check-backend-function 'request-head group)
13256                       (gnus-request-head id group)
13257                     (gnus-request-article id group)))
13258         (save-excursion
13259           (set-buffer nntp-server-buffer)
13260           (and (search-forward "\n\n" nil t)
13261                (delete-region (1- (point)) (point-max)))
13262           (goto-char (point-max))
13263           (insert ".\n")
13264           (goto-char (point-min))
13265           (insert "211 "
13266                   (int-to-string
13267                    (cond
13268                     ((numberp id)
13269                      id)
13270                     ((cdr where)
13271                      (cdr where))
13272                     (t
13273                      gnus-reffed-article-number)))
13274                   " Article retrieved.\n"))
13275         (if (not (setq header (car (gnus-get-newsgroup-headers))))
13276             () ; Malformed head.
13277           (if (and (stringp id)
13278                    (not (string= (gnus-group-real-name group)
13279                                  (car where))))
13280               ;; If we fetched by Message-ID and the article came
13281               ;; from a different group, we fudge some bogus article
13282               ;; numbers for this article.
13283               (mail-header-set-number header gnus-reffed-article-number))
13284           (decf gnus-reffed-article-number)
13285           (push header gnus-newsgroup-headers)
13286           (setq gnus-current-headers header)
13287           (push (mail-header-number header) gnus-newsgroup-limit)
13288           header)))))
13289
13290 (defun gnus-article-prepare (article &optional all-headers header)
13291   "Prepare ARTICLE in article mode buffer.
13292 ARTICLE should either be an article number or a Message-ID.
13293 If ARTICLE is an id, HEADER should be the article headers.
13294 If ALL-HEADERS is non-nil, no headers are hidden."
13295   (save-excursion
13296     ;; Make sure we start in a summary buffer.
13297     (unless (eq major-mode 'gnus-summary-mode)
13298       (set-buffer gnus-summary-buffer))
13299     (setq gnus-summary-buffer (current-buffer))
13300     ;; Make sure the connection to the server is alive.
13301     (unless (gnus-server-opened
13302              (gnus-find-method-for-group gnus-newsgroup-name))
13303       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
13304       (gnus-request-group gnus-newsgroup-name t))
13305     (let* ((article (if header (mail-header-number header) article))
13306            (summary-buffer (current-buffer))
13307            (internal-hook gnus-article-internal-prepare-hook)
13308            (group gnus-newsgroup-name)
13309            result)
13310       (save-excursion
13311         (gnus-article-setup-buffer)
13312         (set-buffer gnus-article-buffer)
13313         ;; Deactivate active regions.
13314         (when (and (boundp 'transient-mark-mode)
13315                    transient-mark-mode)
13316           (setq mark-active nil))
13317         (if (not (setq result (let ((buffer-read-only nil))
13318                                 (gnus-request-article-this-buffer
13319                                  article group))))
13320             ;; There is no such article.
13321             (save-excursion
13322               (when (and (numberp article)
13323                          (not (memq article gnus-newsgroup-sparse)))
13324                 (setq gnus-article-current
13325                       (cons gnus-newsgroup-name article))
13326                 (set-buffer gnus-summary-buffer)
13327                 (setq gnus-current-article article)
13328                 (gnus-summary-mark-article article gnus-canceled-mark))
13329               (unless (memq article gnus-newsgroup-sparse)
13330                 (gnus-message
13331                  1 "No such article (may have expired or been canceled)")
13332                 (ding)
13333                 nil))
13334           (if (or (eq result 'pseudo) (eq result 'nneething))
13335               (progn
13336                 (save-excursion
13337                   (set-buffer summary-buffer)
13338                   (setq gnus-last-article gnus-current-article
13339                         gnus-newsgroup-history (cons gnus-current-article
13340                                                      gnus-newsgroup-history)
13341                         gnus-current-article 0
13342                         gnus-current-headers nil
13343                         gnus-article-current nil)
13344                   (if (eq result 'nneething)
13345                       (gnus-configure-windows 'summary)
13346                     (gnus-configure-windows 'article))
13347                   (gnus-set-global-variables))
13348                 (gnus-set-mode-line 'article))
13349             ;; The result from the `request' was an actual article -
13350             ;; or at least some text that is now displayed in the
13351             ;; article buffer.
13352             (if (and (numberp article)
13353                      (not (eq article gnus-current-article)))
13354                 ;; Seems like a new article has been selected.
13355                 ;; `gnus-current-article' must be an article number.
13356                 (save-excursion
13357                   (set-buffer summary-buffer)
13358                   (setq gnus-last-article gnus-current-article
13359                         gnus-newsgroup-history (cons gnus-current-article
13360                                                      gnus-newsgroup-history)
13361                         gnus-current-article article
13362                         gnus-current-headers
13363                         (gnus-summary-article-header gnus-current-article)
13364                         gnus-article-current
13365                         (cons gnus-newsgroup-name gnus-current-article))
13366                   (unless (vectorp gnus-current-headers)
13367                     (setq gnus-current-headers nil))
13368                   (gnus-summary-show-thread)
13369                   (run-hooks 'gnus-mark-article-hook)
13370                   (gnus-set-mode-line 'summary)
13371                   (and (gnus-visual-p 'article-highlight 'highlight)
13372                        (run-hooks 'gnus-visual-mark-article-hook))
13373                   ;; Set the global newsgroup variables here.
13374                   ;; Suggested by Jim Sisolak
13375                   ;; <sisolak@trans4.neep.wisc.edu>.
13376                   (gnus-set-global-variables)
13377                   (setq gnus-have-all-headers
13378                         (or all-headers gnus-show-all-headers))
13379                   (and gnus-use-cache
13380                        (vectorp (gnus-summary-article-header article))
13381                        (gnus-cache-possibly-enter-article
13382                         group article
13383                         (gnus-summary-article-header article)
13384                         (memq article gnus-newsgroup-marked)
13385                         (memq article gnus-newsgroup-dormant)
13386                         (memq article gnus-newsgroup-unreads)))))
13387             ;; Hooks for getting information from the article.
13388             ;; This hook must be called before being narrowed.
13389             (let (buffer-read-only)
13390               (run-hooks 'internal-hook)
13391               (run-hooks 'gnus-article-prepare-hook)
13392               ;; Decode MIME message.
13393               (if gnus-show-mime
13394                   (if (or (not gnus-strict-mime)
13395                           (gnus-fetch-field "Mime-Version"))
13396                       (funcall gnus-show-mime-method)
13397                     (funcall gnus-decode-encoded-word-method)))
13398               ;; Perform the article display hooks.
13399               (run-hooks 'gnus-article-display-hook))
13400             ;; Do page break.
13401             (goto-char (point-min))
13402             (and gnus-break-pages (gnus-narrow-to-page))
13403             (gnus-set-mode-line 'article)
13404             (gnus-configure-windows 'article)
13405             (goto-char (point-min))
13406             t))))))
13407
13408 (defun gnus-article-show-all-headers ()
13409   "Show all article headers in article mode buffer."
13410   (save-excursion
13411     (gnus-article-setup-buffer)
13412     (set-buffer gnus-article-buffer)
13413     (let ((buffer-read-only nil))
13414       (remove-text-properties (point-min) (point-max)
13415                               gnus-hidden-properties))))
13416
13417 (defun gnus-article-hide-headers-if-wanted ()
13418   "Hide unwanted headers if `gnus-have-all-headers' is nil.
13419 Provided for backwards compatibility."
13420   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
13421       gnus-inhibit-hiding
13422       (gnus-article-hide-headers)))
13423
13424 (defun gnus-article-hide-headers (&optional arg delete)
13425   "Toggle whether to hide unwanted headers and possibly sort them as well.
13426 If given a negative prefix, always show; if given a positive prefix,
13427 always hide."
13428   (interactive "P")
13429   (unless (gnus-article-check-hidden-text 'headers arg)
13430     ;; This function might be inhibited.
13431     (unless gnus-inhibit-hiding
13432       (save-excursion
13433         (set-buffer gnus-article-buffer)
13434         (save-restriction
13435           (let ((buffer-read-only nil)
13436                 (ignored (when (not (stringp gnus-visible-headers))
13437                            (cond ((stringp gnus-ignored-headers)
13438                                   gnus-ignored-headers)
13439                                  ((listp gnus-ignored-headers)
13440                                   (mapconcat 'identity gnus-ignored-headers
13441                                              "\\|")))))
13442                 (visible
13443                  (cond ((stringp gnus-visible-headers)
13444                         gnus-visible-headers)
13445                        ((and gnus-visible-headers
13446                              (listp gnus-visible-headers))
13447                         (mapconcat 'identity gnus-visible-headers "\\|"))))
13448                 want-list beg want-l)
13449             ;; First we narrow to just the headers.
13450             (widen)
13451             (goto-char (point-min))
13452             ;; Hide any "From " lines at the beginning of (mail) articles.
13453             (while (looking-at "From ")
13454               (forward-line 1))
13455             (unless (bobp)
13456               (add-text-properties
13457                (point-min) (point)
13458                (nconc (list 'gnus-type 'headers) gnus-hidden-properties)))
13459             ;; Then treat the rest of the header lines.
13460             (narrow-to-region
13461              (point)
13462              (progn (search-forward "\n\n" nil t) (forward-line -1) (point)))
13463             ;; Then we use the two regular expressions
13464             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
13465             ;; select which header lines is to remain visible in the
13466             ;; article buffer.
13467             (goto-char (point-min))
13468             (while (re-search-forward "^[^ \t]*:" nil t)
13469               (beginning-of-line)
13470               ;; We add the headers we want to keep to a list and delete
13471               ;; them from the buffer.
13472               (if (or (and visible (looking-at visible))
13473                       (and ignored (not (looking-at ignored))))
13474                   (progn
13475                     (push (buffer-substring
13476                            (setq beg (point))
13477                            (progn
13478                              (forward-line 1)
13479                              ;; Be sure to get multi-line headers...
13480                              (re-search-forward "^[^ \t]*:" nil t)
13481                              (beginning-of-line)
13482                              (point)))
13483                           want-list)
13484                     (delete-region beg (point)))
13485                 (forward-line 1)))
13486             ;; Sort the headers that we want to display.
13487             (setq want-list (sort want-list 'gnus-article-header-less))
13488             (goto-char (point-min))
13489             (while want-list
13490               (insert (pop want-list)))
13491             ;; We make the unwanted headers invisible.
13492             (if delete
13493                 (delete-region (point-min) (point-max))
13494               ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
13495               (add-text-properties
13496                (point) (point-max)
13497                (nconc (list 'gnus-type 'headers)
13498                       gnus-hidden-properties)))))))))
13499
13500 (defsubst gnus-article-header-rank (header)
13501   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
13502   (let ((list gnus-sorted-header-list)
13503         (i 0))
13504     (while list
13505       (when (string-match (car list) header)
13506         (setq list nil))
13507       (setq list (cdr list))
13508       (incf i))
13509     i))
13510
13511 (defun gnus-article-header-less (h1 h2)
13512   "Say whether string H1 is \"less\" than string H2."
13513   (< (gnus-article-header-rank h1)
13514      (gnus-article-header-rank h2)))
13515
13516 (defun gnus-article-hide-boring-headers (&optional arg)
13517   "Toggle hiding of headers that aren't very interesting.
13518 If given a negative prefix, always show; if given a positive prefix,
13519 always hide."
13520   (interactive "P")
13521   (unless (gnus-article-check-hidden-text 'boring-headers arg)
13522     (save-excursion
13523       (set-buffer gnus-article-buffer)
13524       (save-restriction
13525         (let ((buffer-read-only nil)
13526               (list gnus-boring-article-headers)
13527               (inhibit-point-motion-hooks t)
13528               elem)
13529           (nnheader-narrow-to-headers)
13530           (while list
13531             (setq elem (pop list))
13532             (goto-char (point-min))
13533             (cond
13534              ;; Hide empty headers.
13535              ((eq elem 'empty)
13536               (while (re-search-forward "^[^:]+:[ \t]\n[^ \t]" nil t)
13537                 (forward-line -1)
13538                 (add-text-properties
13539                  (progn (beginning-of-line) (point))
13540                  (progn 
13541                    (end-of-line)
13542                    (if (re-search-forward "^[^ \t]" nil t)
13543                        (match-beginning 0)
13544                      (point-max)))
13545                  (nconc (list 'gnus-type 'boring-headers)
13546                         gnus-hidden-properties))))
13547              ;; Hide boring Newsgroups header.
13548              ((eq elem 'newsgroups)
13549               (when (equal (mail-fetch-field "newsgroups")
13550                            (gnus-group-real-name gnus-newsgroup-name))
13551                 (gnus-article-hide-header "newsgroups")))
13552              ((eq elem 'followup-to)
13553               (when (equal (mail-fetch-field "followup-to")
13554                            (mail-fetch-field "newsgroups"))
13555                 (gnus-article-hide-header "followup-to")))
13556              ((eq elem 'reply-to)
13557               (let ((from (mail-fetch-field "from"))
13558                     (reply-to (mail-fetch-field "reply-to")))
13559                 (when (and
13560                        from reply-to
13561                        (equal 
13562                         (nth 1 (mail-extract-address-components from))
13563                         (nth 1 (mail-extract-address-components reply-to))))
13564                   (gnus-article-hide-header "reply-to"))))
13565              ((eq elem 'date)
13566               (let ((date (mail-fetch-field "date")))
13567                 (when (and date
13568                            (< (gnus-days-between date (current-time-string))
13569                               4))
13570                   (gnus-article-hide-header "date")))))))))))
13571
13572 (defun gnus-article-hide-header (header)
13573   (save-excursion
13574     (goto-char (point-min))
13575     (when (re-search-forward (concat "^" header ":") nil t)
13576       (add-text-properties
13577        (progn (beginning-of-line) (point))
13578        (progn 
13579          (end-of-line)
13580          (if (re-search-forward "^[^ \t]" nil t)
13581              (match-beginning 0)
13582            (point-max)))
13583        (nconc (list 'gnus-type 'boring-headers)
13584               gnus-hidden-properties)))))
13585
13586 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
13587 (defun gnus-article-treat-overstrike ()
13588   "Translate overstrikes into bold text."
13589   (interactive)
13590   (save-excursion
13591     (set-buffer gnus-article-buffer)
13592     (let ((buffer-read-only nil))
13593       (while (search-forward "\b" nil t)
13594         (let ((next (following-char))
13595               (previous (char-after (- (point) 2))))
13596           (cond ((eq next previous)
13597                  (put-text-property (- (point) 2) (point) 'invisible t)
13598                  (put-text-property (point) (1+ (point)) 'face 'bold))
13599                 ((eq next ?_)
13600                  (put-text-property (1- (point)) (1+ (point)) 'invisible t)
13601                  (put-text-property
13602                   (- (point) 2) (1- (point)) 'face 'underline))
13603                 ((eq previous ?_)
13604                  (put-text-property (- (point) 2) (point) 'invisible t)
13605                  (put-text-property
13606                   (point) (1+ (point))  'face 'underline))))))))
13607
13608 (defun gnus-article-word-wrap ()
13609   "Format too long lines."
13610   (interactive)
13611   (save-excursion
13612     (set-buffer gnus-article-buffer)
13613     (let ((buffer-read-only nil)
13614           p)
13615       (widen)
13616       (goto-char (point-min))
13617       (search-forward "\n\n" nil t)
13618       (end-of-line 1)
13619       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
13620             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
13621             (adaptive-fill-mode t))
13622         (while (not (eobp))
13623           (and (>= (current-column) (min fill-column (window-width)))
13624                (/= (preceding-char) ?:)
13625                (fill-paragraph nil))
13626           (end-of-line 2))))))
13627
13628 (defun gnus-article-remove-cr ()
13629   "Remove carriage returns from an article."
13630   (interactive)
13631   (save-excursion
13632     (set-buffer gnus-article-buffer)
13633     (let ((buffer-read-only nil))
13634       (goto-char (point-min))
13635       (while (search-forward "\r" nil t)
13636         (replace-match "" t t)))))
13637
13638 (defun gnus-article-remove-trailing-blank-lines ()
13639   "Remove all trailing blank lines from the article."
13640   (interactive)
13641   (save-excursion
13642     (set-buffer gnus-article-buffer)
13643     (let ((buffer-read-only nil))
13644       (goto-char (point-max))
13645       (delete-region
13646        (point)
13647        (progn
13648          (while (looking-at "^[ \t]*$")
13649            (forward-line -1))
13650          (forward-line 1)
13651          (point))))))
13652
13653 (defun gnus-article-display-x-face (&optional force)
13654   "Look for an X-Face header and display it if present."
13655   (interactive (list 'force))
13656   (save-excursion
13657     (set-buffer gnus-article-buffer)
13658     ;; Delete the old process, if any.
13659     (when (process-status "gnus-x-face")
13660       (delete-process "gnus-x-face"))
13661     (let ((inhibit-point-motion-hooks t)
13662           (case-fold-search nil)
13663           from)
13664       (save-restriction
13665         (nnheader-narrow-to-headers)
13666         (setq from (mail-fetch-field "from"))
13667         (goto-char (point-min))
13668         (when (and gnus-article-x-face-command
13669                    (or force
13670                        ;; Check whether this face is censored.
13671                        (not gnus-article-x-face-too-ugly)
13672                        (and gnus-article-x-face-too-ugly from
13673                             (not (string-match gnus-article-x-face-too-ugly
13674                                                from))))
13675                    ;; Has to be present.
13676                    (re-search-forward "^X-Face: " nil t))
13677           ;; We now have the area of the buffer where the X-Face is stored.
13678           (let ((beg (point))
13679                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
13680             ;; We display the face.
13681             (if (symbolp gnus-article-x-face-command)
13682                 ;; The command is a lisp function, so we call it.
13683                 (if (gnus-functionp gnus-article-x-face-command)
13684                     (funcall gnus-article-x-face-command beg end)
13685                   (error "%s is not a function" gnus-article-x-face-command))
13686               ;; The command is a string, so we interpret the command
13687               ;; as a, well, command, and fork it off.
13688               (let ((process-connection-type nil))
13689                 (process-kill-without-query
13690                  (start-process
13691                   "gnus-x-face" nil "sh" "-c" gnus-article-x-face-command))
13692                 (process-send-region "gnus-x-face" beg end)
13693                 (process-send-eof "gnus-x-face")))))))))
13694
13695 (defun gnus-headers-decode-quoted-printable ()
13696   "Hack to remove QP encoding from headers."
13697   (let ((case-fold-search t)
13698         (inhibit-point-motion-hooks t)
13699         string)
13700     (goto-char (point-min))
13701     (while (re-search-forward "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
13702       (setq string (match-string 1))
13703       (narrow-to-region (match-beginning 0) (match-end 0))
13704       (delete-region (point-min) (point-max))
13705       (insert string)
13706       (gnus-mime-decode-quoted-printable (goto-char (point-min)) (point-max))
13707       (subst-char-in-region (point-min) (point-max) ?_ ? )
13708       (widen)
13709       (goto-char (point-min)))))
13710
13711 (defun gnus-article-de-quoted-unreadable (&optional force)
13712   "Do a naive translation of a quoted-printable-encoded article.
13713 This is in no way, shape or form meant as a replacement for real MIME
13714 processing, but is simply a stop-gap measure until MIME support is
13715 written.
13716 If FORCE, decode the article whether it is marked as quoted-printable
13717 or not."
13718   (interactive (list 'force))
13719   (save-excursion
13720     (set-buffer gnus-article-buffer)
13721     (let ((case-fold-search t)
13722           (buffer-read-only nil)
13723           (type (gnus-fetch-field "content-transfer-encoding")))
13724       (when (or force
13725                 (and type (string-match "quoted-printable" type)))
13726         (gnus-headers-decode-quoted-printable)
13727         (goto-char (point-min))
13728         (search-forward "\n\n" nil 'move)
13729         (gnus-mime-decode-quoted-printable (point) (point-max))))))
13730
13731 (defun gnus-mime-decode-quoted-printable (from to)
13732   "Decode Quoted-Printable in the region between FROM and TO."
13733   (goto-char from)
13734   (while (search-forward "=" to t)
13735     (cond ((eq (following-char) ?\n)
13736            (delete-char -1)
13737            (delete-char 1))
13738           ((looking-at "[0-9A-F][0-9A-F]")
13739            (delete-char -1)
13740            (insert (hexl-hex-string-to-integer
13741                     (buffer-substring (point) (+ 2 (point)))))
13742            (delete-char 2))
13743           ((looking-at "=")
13744            (delete-char 1))
13745           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
13746
13747 (defun gnus-article-hide-pgp (&optional arg)
13748   "Toggle hiding of any PGP headers and signatures in the current article.
13749 If given a negative prefix, always show; if given a positive prefix,
13750 always hide."
13751   (interactive "P")
13752   (unless (gnus-article-check-hidden-text 'pgp arg)
13753     (save-excursion
13754       (set-buffer gnus-article-buffer)
13755       (let ((props (nconc (list 'gnus-type 'pgp) gnus-hidden-properties))
13756             buffer-read-only beg end)
13757         (widen)
13758         (goto-char (point-min))
13759         ;; Hide the "header".
13760         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
13761              (add-text-properties (match-beginning 0) (match-end 0) props))
13762         (setq beg (point))
13763         ;; Hide the actual signature.
13764         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
13765              (setq end (match-beginning 0))
13766              (add-text-properties
13767               (match-beginning 0)
13768               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
13769                   (match-end 0)
13770                 ;; Perhaps we shouldn't hide to the end of the buffer
13771                 ;; if there is no end to the signature?
13772                 (point-max))
13773               props))
13774         ;; Hide "- " PGP quotation markers.
13775         (when (and beg end)
13776           (narrow-to-region beg end)
13777           (goto-char (point-min))
13778           (while (re-search-forward "^- " nil t)
13779             (add-text-properties (match-beginning 0) (match-end 0) props))
13780           (widen))))))
13781
13782 (defun gnus-article-hide-signature (&optional arg)
13783   "Hide the signature in the current article.
13784 If given a negative prefix, always show; if given a positive prefix,
13785 always hide."
13786   (interactive "P")
13787   (unless (gnus-article-check-hidden-text 'signature arg)
13788     (save-excursion
13789       (set-buffer gnus-article-buffer)
13790       (save-restriction
13791         (let ((buffer-read-only nil))
13792           (when (gnus-narrow-to-signature)
13793             (add-text-properties
13794              (point-min) (point-max)
13795              (nconc (list 'gnus-type 'signature)
13796                     gnus-hidden-properties))))))))
13797
13798 (defun gnus-narrow-to-signature ()
13799   "Narrow to the signature."
13800   (widen)
13801   (goto-char (point-max))
13802   (when (re-search-backward gnus-signature-separator nil t)
13803     (forward-line 1)
13804     (when (or (null gnus-signature-limit)
13805               (and (numberp gnus-signature-limit)
13806                    (< (- (point-max) (point)) gnus-signature-limit))
13807               (and (gnus-functionp gnus-signature-limit)
13808                    (funcall gnus-signature-limit))
13809               (and (stringp gnus-signature-limit)
13810                    (not (re-search-forward gnus-signature-limit nil t))))
13811       (narrow-to-region (point) (point-max))
13812       t)))
13813
13814 (defun gnus-article-check-hidden-text (type arg)
13815   "Return nil if hiding is necessary."
13816   (save-excursion
13817     (set-buffer gnus-article-buffer)
13818     (let ((hide (gnus-article-hidden-text-p type)))
13819       (cond ((or (and (null arg) (eq hide 'hidden))
13820                  (and arg (< 0 (prefix-numeric-value arg))))
13821              (gnus-article-show-hidden-text type))
13822             ((eq hide 'shown)
13823              (gnus-article-show-hidden-text type t))
13824             (t nil)))))
13825
13826 (defun gnus-article-hidden-text-p (type)
13827   "Say whether the current buffer contains hidden text of type TYPE."
13828   (let ((pos (text-property-any (point-min) (point-max) 'gnus-type type))
13829         prop)
13830     (when pos
13831       (if (get-text-property pos 'invisible)
13832           'hidden
13833         'shown))))
13834
13835 (defun gnus-article-hide (&optional arg force)
13836   "Hide all the gruft in the current article.
13837 This means that PGP stuff, signatures, cited text and (some)
13838 headers will be hidden.
13839 If given a prefix, show the hidden text instead."
13840   (interactive (list current-prefix-arg 'force))
13841   (gnus-article-hide-headers arg)
13842   (gnus-article-hide-pgp arg)
13843   (gnus-article-hide-citation-maybe arg force)
13844   (gnus-article-hide-signature arg))
13845
13846 (defun gnus-article-show-hidden-text (type &optional hide)
13847   "Show all hidden text of type TYPE.
13848 If HIDE, hide the text instead."
13849   (save-excursion
13850     (set-buffer gnus-article-buffer)
13851     (let ((buffer-read-only nil)
13852           (inhibit-point-motion-hooks t)
13853           (beg (point-min)))
13854       (while (gnus-goto-char (text-property-any
13855                               beg (point-max) 'gnus-type type))
13856         (setq beg (point))
13857         (forward-char)
13858         (if hide
13859             (add-text-properties beg (point) gnus-hidden-properties)
13860           (remove-text-properties beg (point) gnus-hidden-properties))
13861         (setq beg (point)))
13862       t)))
13863
13864 (defvar gnus-article-time-units
13865   `((year . ,(* 365.25 24 60 60))
13866     (week . ,(* 7 24 60 60))
13867     (day . ,(* 24 60 60))
13868     (hour . ,(* 60 60))
13869     (minute . 60)
13870     (second . 1))
13871   "Mapping from time units to seconds.")
13872
13873 (defun gnus-article-date-ut (&optional type highlight)
13874   "Convert DATE date to universal time in the current article.
13875 If TYPE is `local', convert to local time; if it is `lapsed', output
13876 how much time has lapsed since DATE."
13877   (interactive (list 'ut t))
13878   (let* ((header (or gnus-current-headers
13879                      (gnus-summary-article-header) ""))
13880          (date (and (vectorp header) (mail-header-date header)))
13881          (date-regexp "^Date: \\|^X-Sent: ")
13882          (now (current-time))
13883          (inhibit-point-motion-hooks t))
13884     (when (and date (not (string= date "")))
13885       (save-excursion
13886         (set-buffer gnus-article-buffer)
13887         (save-restriction
13888           (nnheader-narrow-to-headers)
13889           (let ((buffer-read-only nil))
13890             ;; Delete any old Date headers.
13891             (if (zerop (nnheader-remove-header date-regexp t))
13892                 (beginning-of-line)
13893               (goto-char (point-max)))
13894             (insert
13895              (cond
13896               ;; Convert to the local timezone.  We have to slap a
13897               ;; `condition-case' round the calls to the timezone
13898               ;; functions since they aren't particularly resistant to
13899               ;; buggy dates.
13900               ((eq type 'local)
13901                (concat "Date: " (condition-case ()
13902                                     (timezone-make-date-arpa-standard date)
13903                                   (error date))
13904                        "\n"))
13905               ;; Convert to Universal Time.
13906               ((eq type 'ut)
13907                (concat "Date: "
13908                        (condition-case ()
13909                            (timezone-make-date-arpa-standard date nil "UT")
13910                          (error date))
13911                        "\n"))
13912               ;; Get the original date from the article.
13913               ((eq type 'original)
13914                (concat "Date: " date "\n"))
13915               ;; Do an X-Sent lapsed format.
13916               ((eq type 'lapsed)
13917                ;; If the date is seriously mangled, the timezone
13918                ;; functions are liable to bug out, so we condition-case
13919                ;; the entire thing.
13920                (let* ((real-time
13921                        (condition-case ()
13922                            (gnus-time-minus
13923                             (gnus-encode-date
13924                              (timezone-make-date-arpa-standard
13925                               (current-time-string now)
13926                               (current-time-zone now) "UT"))
13927                             (gnus-encode-date
13928                              (timezone-make-date-arpa-standard
13929                               date nil "UT")))
13930                          (error '(0 0))))
13931                       (real-sec (+ (* (float (car real-time)) 65536)
13932                                    (cadr real-time)))
13933                       (sec (abs real-sec))
13934                       num prev)
13935                  (if (zerop sec)
13936                      "X-Sent: Now\n"
13937                    (concat
13938                     "X-Sent: "
13939                     ;; This is a bit convoluted, but basically we go
13940                     ;; through the time units for years, weeks, etc,
13941                     ;; and divide things to see whether that results
13942                     ;; in positive answers.
13943                     (mapconcat
13944                      (lambda (unit)
13945                        (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
13946                            ;; The (remaining) seconds are too few to
13947                            ;; be divided into this time unit.
13948                            ""
13949                          ;; It's big enough, so we output it.
13950                          (setq sec (- sec (* num (cdr unit))))
13951                          (prog1
13952                              (concat (if prev ", " "") (int-to-string
13953                                                         (floor num))
13954                                      " " (symbol-name (car unit))
13955                                      (if (> num 1) "s" ""))
13956                            (setq prev t))))
13957                      gnus-article-time-units "")
13958                     ;; If dates are odd, then it might appear like the
13959                     ;; article was sent in the future.
13960                     (if (> real-sec 0)
13961                         " ago\n"
13962                       " in the future\n")))))
13963               (t
13964                (error "Unknown conversion type: %s" type)))))
13965           ;; Do highlighting.
13966           (when (and highlight (gnus-visual-p 'article-highlight 'highlight))
13967             (gnus-article-highlight-headers)))))))
13968
13969 (defun gnus-article-date-local (&optional highlight)
13970   "Convert the current article date to the local timezone."
13971   (interactive (list t))
13972   (gnus-article-date-ut 'local highlight))
13973
13974 (defun gnus-article-date-original (&optional highlight)
13975   "Convert the current article date to what it was originally.
13976 This is only useful if you have used some other date conversion
13977 function and want to see what the date was before converting."
13978   (interactive (list t))
13979   (gnus-article-date-ut 'original highlight))
13980
13981 (defun gnus-article-date-lapsed (&optional highlight)
13982   "Convert the current article date to time lapsed since it was sent."
13983   (interactive (list t))
13984   (gnus-article-date-ut 'lapsed highlight))
13985
13986 (defun gnus-article-maybe-highlight ()
13987   "Do some article highlighting if `gnus-visual' is non-nil."
13988   (if (gnus-visual-p 'article-highlight 'highlight)
13989       (gnus-article-highlight-some)))
13990
13991 ;; Article savers.
13992
13993 (defun gnus-output-to-rmail (file-name)
13994   "Append the current article to an Rmail file named FILE-NAME."
13995   (require 'rmail)
13996   ;; Most of these codes are borrowed from rmailout.el.
13997   (setq file-name (expand-file-name file-name))
13998   (setq rmail-default-rmail-file file-name)
13999   (let ((artbuf (current-buffer))
14000         (tmpbuf (get-buffer-create " *Gnus-output*")))
14001     (save-excursion
14002       (or (get-file-buffer file-name)
14003           (file-exists-p file-name)
14004           (if (gnus-yes-or-no-p
14005                (concat "\"" file-name "\" does not exist, create it? "))
14006               (let ((file-buffer (create-file-buffer file-name)))
14007                 (save-excursion
14008                   (set-buffer file-buffer)
14009                   (rmail-insert-rmail-file-header)
14010                   (let ((require-final-newline nil))
14011                     (write-region (point-min) (point-max) file-name t 1)))
14012                 (kill-buffer file-buffer))
14013             (error "Output file does not exist")))
14014       (set-buffer tmpbuf)
14015       (buffer-disable-undo (current-buffer))
14016       (erase-buffer)
14017       (insert-buffer-substring artbuf)
14018       (gnus-convert-article-to-rmail)
14019       ;; Decide whether to append to a file or to an Emacs buffer.
14020       (let ((outbuf (get-file-buffer file-name)))
14021         (if (not outbuf)
14022             (append-to-file (point-min) (point-max) file-name)
14023           ;; File has been visited, in buffer OUTBUF.
14024           (set-buffer outbuf)
14025           (let ((buffer-read-only nil)
14026                 (msg (and (boundp 'rmail-current-message)
14027                           (symbol-value 'rmail-current-message))))
14028             ;; If MSG is non-nil, buffer is in RMAIL mode.
14029             (if msg
14030                 (progn (widen)
14031                        (narrow-to-region (point-max) (point-max))))
14032             (insert-buffer-substring tmpbuf)
14033             (if msg
14034                 (progn
14035                   (goto-char (point-min))
14036                   (widen)
14037                   (search-backward "\^_")
14038                   (narrow-to-region (point) (point-max))
14039                   (goto-char (1+ (point-min)))
14040                   (rmail-count-new-messages t)
14041                   (rmail-show-message msg)))))))
14042     (kill-buffer tmpbuf)))
14043
14044 (defun gnus-output-to-file (file-name)
14045   "Append the current article to a file named FILE-NAME."
14046   (setq file-name (expand-file-name file-name))
14047   (let ((artbuf (current-buffer))
14048         (tmpbuf (get-buffer-create " *Gnus-output*")))
14049     (save-excursion
14050       (set-buffer tmpbuf)
14051       (buffer-disable-undo (current-buffer))
14052       (erase-buffer)
14053       (insert-buffer-substring artbuf)
14054       ;; Append newline at end of the buffer as separator, and then
14055       ;; save it to file.
14056       (goto-char (point-max))
14057       (insert "\n")
14058       (append-to-file (point-min) (point-max) file-name))
14059     (kill-buffer tmpbuf)))
14060
14061 (defun gnus-convert-article-to-rmail ()
14062   "Convert article in current buffer to Rmail message format."
14063   (let ((buffer-read-only nil))
14064     ;; Convert article directly into Babyl format.
14065     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
14066     (goto-char (point-min))
14067     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
14068     (while (search-forward "\n\^_" nil t) ;single char
14069       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
14070     (goto-char (point-max))
14071     (insert "\^_")))
14072
14073 (defun gnus-narrow-to-page (&optional arg)
14074   "Narrow the article buffer to a page.
14075 If given a numerical ARG, move forward ARG pages."
14076   (interactive "P")
14077   (setq arg (if arg (prefix-numeric-value arg) 0))
14078   (save-excursion
14079     (set-buffer gnus-article-buffer)
14080     (goto-char (point-min))
14081     (widen)
14082     (when (gnus-visual-p 'page-marker)
14083       (let ((buffer-read-only nil))
14084         (gnus-remove-text-with-property 'gnus-prev)
14085         (gnus-remove-text-with-property 'gnus-next)))
14086     (when
14087         (cond ((< arg 0)
14088                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
14089               ((> arg 0)
14090                (re-search-forward page-delimiter nil 'move arg)))
14091       (goto-char (match-end 0)))
14092     (narrow-to-region
14093      (point)
14094      (if (re-search-forward page-delimiter nil 'move)
14095          (match-beginning 0)
14096        (point)))
14097     (when (and (gnus-visual-p 'page-marker)
14098                (not (= (point-min) 1)))
14099       (save-excursion
14100         (goto-char (point-min))
14101         (gnus-insert-prev-page-button)))
14102     (when (and (gnus-visual-p 'page-marker)
14103                (not (= (1- (point-max)) (buffer-size))))
14104       (save-excursion
14105         (goto-char (point-max))
14106         (gnus-insert-next-page-button)))))
14107
14108 ;; Article mode commands
14109
14110 (defun gnus-article-goto-next-page ()
14111   "Show the next page of the article."
14112   (interactive)
14113   (when (gnus-article-next-page)
14114     (gnus-article-read-summary-keys nil ?n)))
14115
14116 (defun gnus-article-goto-prev-page ()
14117   "Show the next page of the article."
14118   (interactive)
14119   (if (bobp) (gnus-article-read-summary-keys nil ?n)
14120     (gnus-article-prev-page nil)))
14121
14122 (defun gnus-article-next-page (&optional lines)
14123   "Show the next page of the current article.
14124 If end of article, return non-nil.  Otherwise return nil.
14125 Argument LINES specifies lines to be scrolled up."
14126   (interactive "p")
14127   (move-to-window-line -1)
14128   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
14129   (if (save-excursion
14130         (end-of-line)
14131         (and (pos-visible-in-window-p)  ;Not continuation line.
14132              (eobp)))
14133       ;; Nothing in this page.
14134       (if (or (not gnus-break-pages)
14135               (save-excursion
14136                 (save-restriction
14137                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
14138           t                             ;Nothing more.
14139         (gnus-narrow-to-page 1)         ;Go to next page.
14140         nil)
14141     ;; More in this page.
14142     (condition-case ()
14143         (scroll-up lines)
14144       (end-of-buffer
14145        ;; Long lines may cause an end-of-buffer error.
14146        (goto-char (point-max))))
14147     (move-to-window-line 0)
14148     nil))
14149
14150 (defun gnus-article-prev-page (&optional lines)
14151   "Show previous page of current article.
14152 Argument LINES specifies lines to be scrolled down."
14153   (interactive "p")
14154   (move-to-window-line 0)
14155   (if (and gnus-break-pages
14156            (bobp)
14157            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
14158       (progn
14159         (gnus-narrow-to-page -1)        ;Go to previous page.
14160         (goto-char (point-max))
14161         (recenter -1))
14162     (prog1
14163         (condition-case ()
14164             (scroll-down lines)
14165           (error nil))
14166       (move-to-window-line 0))))
14167
14168 (defun gnus-article-refer-article ()
14169   "Read article specified by message-id around point."
14170   (interactive)
14171   (let ((point (point)))
14172     (search-forward ">" nil t)          ;Move point to end of "<....>".
14173     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
14174         (let ((message-id (match-string 1)))
14175           (goto-char point)
14176           (set-buffer gnus-summary-buffer)
14177           (gnus-summary-refer-article message-id))
14178       (goto-char (point))
14179       (error "No references around point"))))
14180
14181 (defun gnus-article-show-summary ()
14182   "Reconfigure windows to show summary buffer."
14183   (interactive)
14184   (gnus-configure-windows 'article)
14185   (gnus-summary-goto-subject gnus-current-article))
14186
14187 (defun gnus-article-describe-briefly ()
14188   "Describe article mode commands briefly."
14189   (interactive)
14190   (gnus-message 6
14191                 (substitute-command-keys "\\<gnus-article-mode-map>\\[gnus-article-next-page]:Next page  \\[gnus-article-prev-page]:Prev page  \\[gnus-article-show-summary]:Show summary  \\[gnus-info-find-node]:Run Info  \\[gnus-article-describe-briefly]:This help")))
14192
14193 (defun gnus-article-summary-command ()
14194   "Execute the last keystroke in the summary buffer."
14195   (interactive)
14196   (let ((obuf (current-buffer))
14197         (owin (current-window-configuration))
14198         func)
14199     (switch-to-buffer gnus-summary-buffer 'norecord)
14200     (setq func (lookup-key (current-local-map) (this-command-keys)))
14201     (call-interactively func)
14202     (set-buffer obuf)
14203     (set-window-configuration owin)
14204     (set-window-point (get-buffer-window (current-buffer)) (point))))
14205
14206 (defun gnus-article-summary-command-nosave ()
14207   "Execute the last keystroke in the summary buffer."
14208   (interactive)
14209   (let (func)
14210     (pop-to-buffer gnus-summary-buffer 'norecord)
14211     (setq func (lookup-key (current-local-map) (this-command-keys)))
14212     (call-interactively func)))
14213
14214 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
14215   "Read a summary buffer key sequence and execute it from the article buffer."
14216   (interactive "P")
14217   (let ((nosaves
14218          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
14219            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
14220            "=" "^" "\M-^"))
14221         keys)
14222     (save-excursion
14223       (set-buffer gnus-summary-buffer)
14224       (push (or key last-command-event) unread-command-events)
14225       (setq keys (read-key-sequence nil)))
14226     (message "")
14227
14228     (if (member keys nosaves)
14229         (let (func)
14230           (pop-to-buffer gnus-summary-buffer 'norecord)
14231           (if (setq func (lookup-key (current-local-map) keys))
14232               (call-interactively func)
14233             (ding)))
14234       (let ((obuf (current-buffer))
14235             (owin (current-window-configuration))
14236             (opoint (point))
14237             func in-buffer)
14238         (if not-restore-window
14239             (pop-to-buffer gnus-summary-buffer 'norecord)
14240           (switch-to-buffer gnus-summary-buffer 'norecord))
14241         (setq in-buffer (current-buffer))
14242         (if (setq func (lookup-key (current-local-map) keys))
14243             (call-interactively func)
14244           (ding))
14245         (when (eq in-buffer (current-buffer))
14246           (set-buffer obuf)
14247           (unless not-restore-window
14248             (set-window-configuration owin))
14249           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
14250
14251 \f
14252 ;;;
14253 ;;; Kill file handling.
14254 ;;;
14255
14256 ;;;###autoload
14257 (defalias 'gnus-batch-kill 'gnus-batch-score)
14258 ;;;###autoload
14259 (defun gnus-batch-score ()
14260   "Run batched scoring.
14261 Usage: emacs -batch -l gnus -f gnus-batch-score <newsgroups> ...
14262 Newsgroups is a list of strings in Bnews format.  If you want to score
14263 the comp hierarchy, you'd say \"comp.all\".  If you would not like to
14264 score the alt hierarchy, you'd say \"!alt.all\"."
14265   (interactive)
14266   (let* ((yes-and-no
14267           (gnus-newsrc-parse-options
14268            (apply (function concat)
14269                   (mapcar (lambda (g) (concat g " "))
14270                           command-line-args-left))))
14271          (gnus-expert-user t)
14272          (nnmail-spool-file nil)
14273          (gnus-use-dribble-file nil)
14274          (yes (car yes-and-no))
14275          (no (cdr yes-and-no))
14276          group newsrc entry
14277          ;; Disable verbose message.
14278          gnus-novice-user gnus-large-newsgroup)
14279     ;; Eat all arguments.
14280     (setq command-line-args-left nil)
14281     ;; Start Gnus.
14282     (gnus)
14283     ;; Apply kills to specified newsgroups in command line arguments.
14284     (setq newsrc (cdr gnus-newsrc-alist))
14285     (while newsrc
14286       (setq group (caar newsrc))
14287       (setq entry (gnus-gethash group gnus-newsrc-hashtb))
14288       (if (and (<= (nth 1 (car newsrc)) gnus-level-subscribed)
14289                (and (car entry)
14290                     (or (eq (car entry) t)
14291                         (not (zerop (car entry)))))
14292                (if yes (string-match yes group) t)
14293                (or (null no) (not (string-match no group))))
14294           (progn
14295             (gnus-summary-read-group group nil t nil t)
14296             (and (eq (current-buffer) (get-buffer gnus-summary-buffer))
14297                  (gnus-summary-exit))))
14298       (setq newsrc (cdr newsrc)))
14299     ;; Exit Emacs.
14300     (switch-to-buffer gnus-group-buffer)
14301     (gnus-group-save-newsrc)))
14302
14303 (defun gnus-apply-kill-file ()
14304   "Apply a kill file to the current newsgroup.
14305 Returns the number of articles marked as read."
14306   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
14307           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14308       (gnus-apply-kill-file-internal)
14309     0))
14310
14311 (defun gnus-kill-save-kill-buffer ()
14312   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
14313     (when (get-file-buffer file)
14314       (save-excursion
14315         (set-buffer (get-file-buffer file))
14316         (and (buffer-modified-p) (save-buffer))
14317         (kill-buffer (current-buffer))))))
14318
14319 (defvar gnus-kill-file-name "KILL"
14320   "Suffix of the kill files.")
14321
14322 (defun gnus-newsgroup-kill-file (newsgroup)
14323   "Return the name of a kill file name for NEWSGROUP.
14324 If NEWSGROUP is nil, return the global kill file name instead."
14325   (cond 
14326    ;; The global KILL file is placed at top of the directory.
14327    ((or (null newsgroup)
14328         (string-equal newsgroup ""))
14329     (expand-file-name gnus-kill-file-name
14330                       (or gnus-kill-files-directory "~/News")))
14331    ;; Append ".KILL" to newsgroup name.
14332    ((gnus-use-long-file-name 'not-kill)
14333     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
14334                               "." gnus-kill-file-name)
14335                       (or gnus-kill-files-directory "~/News")))
14336    ;; Place "KILL" under the hierarchical directory.
14337    (t
14338     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
14339                               "/" gnus-kill-file-name)
14340                       (or gnus-kill-files-directory "~/News")))))
14341
14342 \f
14343 ;;;
14344 ;;; Dribble file
14345 ;;;
14346
14347 (defvar gnus-dribble-ignore nil)
14348 (defvar gnus-dribble-eval-file nil)
14349
14350 (defun gnus-dribble-file-name ()
14351   "Return the dribble file for the current .newsrc."
14352   (concat
14353    (if gnus-dribble-directory
14354        (concat (file-name-as-directory gnus-dribble-directory)
14355                (file-name-nondirectory gnus-current-startup-file))
14356      gnus-current-startup-file)
14357    "-dribble"))
14358
14359 (defun gnus-dribble-enter (string)
14360   "Enter STRING into the dribble buffer."
14361   (if (and (not gnus-dribble-ignore)
14362            gnus-dribble-buffer
14363            (buffer-name gnus-dribble-buffer))
14364       (let ((obuf (current-buffer)))
14365         (set-buffer gnus-dribble-buffer)
14366         (insert string "\n")
14367         (set-window-point (get-buffer-window (current-buffer)) (point-max))
14368         (set-buffer obuf))))
14369
14370 (defun gnus-dribble-read-file ()
14371   "Read the dribble file from disk."
14372   (let ((dribble-file (gnus-dribble-file-name)))
14373     (save-excursion
14374       (set-buffer (setq gnus-dribble-buffer
14375                         (get-buffer-create
14376                          (file-name-nondirectory dribble-file))))
14377       (gnus-add-current-to-buffer-list)
14378       (erase-buffer)
14379       (setq buffer-file-name dribble-file)
14380       (auto-save-mode t)
14381       (buffer-disable-undo (current-buffer))
14382       (bury-buffer (current-buffer))
14383       (set-buffer-modified-p nil)
14384       (let ((auto (make-auto-save-file-name))
14385             (gnus-dribble-ignore t))
14386         (when (or (file-exists-p auto) (file-exists-p dribble-file))
14387           ;; Load whichever file is newest -- the auto save file
14388           ;; or the "real" file.
14389           (if (file-newer-than-file-p auto dribble-file)
14390               (insert-file-contents auto)
14391             (insert-file-contents dribble-file))
14392           (unless (zerop (buffer-size))
14393             (set-buffer-modified-p t))
14394           ;; Set the file modes to reflect the .newsrc file modes.
14395           (save-buffer)
14396           (when (file-exists-p gnus-current-startup-file)
14397             (set-file-modes dribble-file
14398                             (file-modes gnus-current-startup-file)))
14399           ;; Possibly eval the file later.
14400           (when (gnus-y-or-n-p
14401                  "Auto-save file exists.  Do you want to read it? ")
14402             (setq gnus-dribble-eval-file t)))))))
14403
14404 (defun gnus-dribble-eval-file ()
14405   (if (not gnus-dribble-eval-file)
14406       ()
14407     (setq gnus-dribble-eval-file nil)
14408     (save-excursion
14409       (let ((gnus-dribble-ignore t))
14410         (set-buffer gnus-dribble-buffer)
14411         (eval-buffer (current-buffer))))))
14412
14413 (defun gnus-dribble-delete-file ()
14414   (if (file-exists-p (gnus-dribble-file-name))
14415       (delete-file (gnus-dribble-file-name)))
14416   (if gnus-dribble-buffer
14417       (save-excursion
14418         (set-buffer gnus-dribble-buffer)
14419         (let ((auto (make-auto-save-file-name)))
14420           (if (file-exists-p auto)
14421               (delete-file auto))
14422           (erase-buffer)
14423           (set-buffer-modified-p nil)))))
14424
14425 (defun gnus-dribble-save ()
14426   (if (and gnus-dribble-buffer
14427            (buffer-name gnus-dribble-buffer))
14428       (save-excursion
14429         (set-buffer gnus-dribble-buffer)
14430         (save-buffer))))
14431
14432 (defun gnus-dribble-clear ()
14433   (save-excursion
14434     (if (gnus-buffer-exists-p gnus-dribble-buffer)
14435         (progn
14436           (set-buffer gnus-dribble-buffer)
14437           (erase-buffer)
14438           (set-buffer-modified-p nil)
14439           (setq buffer-saved-size (buffer-size))))))
14440
14441 \f
14442 ;;;
14443 ;;; Server Communication
14444 ;;;
14445
14446 (defun gnus-start-news-server (&optional confirm)
14447   "Open a method for getting news.
14448 If CONFIRM is non-nil, the user will be asked for an NNTP server."
14449   (let (how)
14450     (if gnus-current-select-method
14451         ;; Stream is already opened.
14452         nil
14453       ;; Open NNTP server.
14454       (if (null gnus-nntp-service) (setq gnus-nntp-server nil))
14455       (if confirm
14456           (progn
14457             ;; Read server name with completion.
14458             (setq gnus-nntp-server
14459                   (completing-read "NNTP server: "
14460                                    (mapcar (lambda (server) (list server))
14461                                            (cons (list gnus-nntp-server)
14462                                                  gnus-secondary-servers))
14463                                    nil nil gnus-nntp-server))))
14464
14465       (if (and gnus-nntp-server
14466                (stringp gnus-nntp-server)
14467                (not (string= gnus-nntp-server "")))
14468           (setq gnus-select-method
14469                 (cond ((or (string= gnus-nntp-server "")
14470                            (string= gnus-nntp-server "::"))
14471                        (list 'nnspool (system-name)))
14472                       ((string-match "^:" gnus-nntp-server)
14473                        (list 'nnmh gnus-nntp-server
14474                              (list 'nnmh-directory
14475                                    (file-name-as-directory
14476                                     (expand-file-name
14477                                      (concat "~/" (substring
14478                                                    gnus-nntp-server 1)))))
14479                              (list 'nnmh-get-new-mail nil)))
14480                       (t
14481                        (list 'nntp gnus-nntp-server)))))
14482
14483       (setq how (car gnus-select-method))
14484       (cond ((eq how 'nnspool)
14485              (require 'nnspool)
14486              (gnus-message 5 "Looking up local news spool..."))
14487             ((eq how 'nnmh)
14488              (require 'nnmh)
14489              (gnus-message 5 "Looking up mh spool..."))
14490             (t
14491              (require 'nntp)))
14492       (setq gnus-current-select-method gnus-select-method)
14493       (run-hooks 'gnus-open-server-hook)
14494       (or
14495        ;; gnus-open-server-hook might have opened it
14496        (gnus-server-opened gnus-select-method)
14497        (gnus-open-server gnus-select-method)
14498        (gnus-y-or-n-p
14499         (format
14500          "%s (%s) open error: '%s'.     Continue? "
14501          (car gnus-select-method) (cadr gnus-select-method)
14502          (gnus-status-message gnus-select-method)))
14503        (progn
14504          (gnus-message 1 "Couldn't open server on %s"
14505                        (nth 1 gnus-select-method))
14506          (ding)
14507          nil)))))
14508
14509 (defun gnus-check-group (group)
14510   "Try to make sure that the server where GROUP exists is alive."
14511   (let ((method (gnus-find-method-for-group group)))
14512     (or (gnus-server-opened method)
14513         (gnus-open-server method))))
14514
14515 (defun gnus-check-server (&optional method silent)
14516   "Check whether the connection to METHOD is down.
14517 If METHOD is nil, use `gnus-select-method'.
14518 If it is down, start it up (again)."
14519   (let ((method (or method gnus-select-method)))
14520     ;; Transform virtual server names into select methods.
14521     (when (stringp method)
14522       (setq method (gnus-server-to-method method)))
14523     (if (gnus-server-opened method)
14524         ;; The stream is already opened.
14525         t
14526       ;; Open the server.
14527       (unless silent
14528         (gnus-message 5 "Opening %s server%s..." (car method)
14529                       (if (equal (nth 1 method) "") ""
14530                         (format " on %s" (nth 1 method)))))
14531       (run-hooks 'gnus-open-server-hook)
14532       (prog1
14533           (gnus-open-server method)
14534         (unless silent
14535           (message ""))))))
14536
14537 (defun gnus-get-function (method function)
14538   "Return a function symbol based on METHOD and FUNCTION."
14539   ;; Translate server names into methods.
14540   (unless method
14541     (error "Attempted use of a nil select method"))
14542   (when (stringp method)
14543     (setq method (gnus-server-to-method method)))
14544   (let ((func (intern (format "%s-%s" (car method) function))))
14545     ;; If the functions isn't bound, we require the backend in
14546     ;; question.
14547     (unless (fboundp func)
14548       (require (car method))
14549       (unless (fboundp func)
14550         ;; This backend doesn't implement this function.
14551         (error "No such function: %s" func)))
14552     func))
14553
14554 \f
14555 ;;;
14556 ;;; Interface functions to the backends.
14557 ;;;
14558
14559 (defun gnus-open-server (method)
14560   "Open a connection to METHOD."
14561   (let ((elem (assoc method gnus-opened-servers)))
14562     ;; If this method was previously denied, we just return nil.
14563     (if (eq (nth 1 elem) 'denied)
14564         (progn
14565           (gnus-message 1 "Denied server")
14566           nil)
14567       ;; Open the server.
14568       (let ((result
14569              (funcall (gnus-get-function method 'open-server)
14570                       (nth 1 method) (nthcdr 2 method))))
14571         ;; If this hasn't been opened before, we add it to the list.
14572         (unless elem
14573           (setq elem (list method nil)
14574                 gnus-opened-servers (cons elem gnus-opened-servers)))
14575         ;; Set the status of this server.
14576         (setcar (cdr elem) (if result 'ok 'denied))
14577         ;; Return the result from the "open" call.
14578         result))))
14579
14580 (defun gnus-close-server (method)
14581   "Close the connection to METHOD."
14582   (funcall (gnus-get-function method 'close-server) (nth 1 method)))
14583
14584 (defun gnus-request-list (method)
14585   "Request the active file from METHOD."
14586   (funcall (gnus-get-function method 'request-list) (nth 1 method)))
14587
14588 (defun gnus-request-list-newsgroups (method)
14589   "Request the newsgroups file from METHOD."
14590   (funcall (gnus-get-function method 'request-list-newsgroups) (nth 1 method)))
14591
14592 (defun gnus-request-newgroups (date method)
14593   "Request all new groups since DATE from METHOD."
14594   (funcall (gnus-get-function method 'request-newgroups)
14595            date (nth 1 method)))
14596
14597 (defun gnus-server-opened (method)
14598   "Check whether a connection to METHOD has been opened."
14599   (funcall (gnus-get-function method 'server-opened) (nth 1 method)))
14600
14601 (defun gnus-status-message (method)
14602   "Return the status message from METHOD.
14603 If METHOD is a string, it is interpreted as a group name.   The method
14604 this group uses will be queried."
14605   (let ((method (if (stringp method) (gnus-find-method-for-group method)
14606                   method)))
14607     (funcall (gnus-get-function method 'status-message) (nth 1 method))))
14608
14609 (defun gnus-request-group (group &optional dont-check method)
14610   "Request GROUP.  If DONT-CHECK, no information is required."
14611   (let ((method (or method (gnus-find-method-for-group group))))
14612     (funcall (gnus-get-function method 'request-group)
14613              (gnus-group-real-name group) (nth 1 method) dont-check)))
14614
14615 (defun gnus-request-asynchronous (group &optional articles)
14616   "Request that GROUP behave asynchronously.
14617 ARTICLES is the `data' of the group."
14618   (let ((method (gnus-find-method-for-group group)))
14619     (funcall (gnus-get-function method 'request-asynchronous)
14620              (gnus-group-real-name group) (nth 1 method) articles)))
14621
14622 (defun gnus-list-active-group (group)
14623   "Request active information on GROUP."
14624   (let ((method (gnus-find-method-for-group group))
14625         (func 'list-active-group))
14626     (when (gnus-check-backend-function func group)
14627       (funcall (gnus-get-function method func)
14628                (gnus-group-real-name group) (nth 1 method)))))
14629
14630 (defun gnus-request-group-description (group)
14631   "Request a description of GROUP."
14632   (let ((method (gnus-find-method-for-group group))
14633         (func 'request-group-description))
14634     (when (gnus-check-backend-function func group)
14635       (funcall (gnus-get-function method func)
14636                (gnus-group-real-name group) (nth 1 method)))))
14637
14638 (defun gnus-close-group (group)
14639   "Request the GROUP be closed."
14640   (let ((method (gnus-find-method-for-group group)))
14641     (funcall (gnus-get-function method 'close-group)
14642              (gnus-group-real-name group) (nth 1 method))))
14643
14644 (defun gnus-retrieve-headers (articles group &optional fetch-old)
14645   "Request headers for ARTICLES in GROUP.
14646 If FETCH-OLD, retrieve all headers (or some subset thereof) in the group."
14647   (let ((method (gnus-find-method-for-group group)))
14648     (if (and gnus-use-cache (numberp (car articles)))
14649         (gnus-cache-retrieve-headers articles group fetch-old)
14650       (funcall (gnus-get-function method 'retrieve-headers)
14651                articles (gnus-group-real-name group) (nth 1 method)
14652                fetch-old))))
14653
14654 (defun gnus-retrieve-groups (groups method)
14655   "Request active information on GROUPS from METHOD."
14656   (funcall (gnus-get-function method 'retrieve-groups) groups (nth 1 method)))
14657
14658 (defun gnus-request-type (group &optional article)
14659   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14660   (let ((method (gnus-find-method-for-group group)))
14661     (if (not (gnus-check-backend-function 'request-type (car method)))
14662         'unknown
14663       (funcall (gnus-get-function method 'request-type)
14664                (gnus-group-real-name group) article))))
14665
14666 (defun gnus-request-update-mark (group article mark)
14667   "Return the type (`post' or `mail') of GROUP (and ARTICLE)."
14668   (let ((method (gnus-find-method-for-group group)))
14669     (if (not (gnus-check-backend-function 'request-update-mark (car method)))
14670         mark
14671       (funcall (gnus-get-function method 'request-update-mark)
14672                (gnus-group-real-name group) article mark))))
14673
14674 (defun gnus-request-article (article group &optional buffer)
14675   "Request the ARTICLE in GROUP.
14676 ARTICLE can either be an article number or an article Message-ID.
14677 If BUFFER, insert the article in that group."
14678   (let ((method (gnus-find-method-for-group group)))
14679     (funcall (gnus-get-function method 'request-article)
14680              article (gnus-group-real-name group) (nth 1 method) buffer)))
14681
14682 (defun gnus-request-head (article group)
14683   "Request the head of ARTICLE in GROUP."
14684   (let ((method (gnus-find-method-for-group group)))
14685     (funcall (gnus-get-function method 'request-head)
14686              article (gnus-group-real-name group) (nth 1 method))))
14687
14688 (defun gnus-request-body (article group)
14689   "Request the body of ARTICLE in GROUP."
14690   (let ((method (gnus-find-method-for-group group)))
14691     (funcall (gnus-get-function method 'request-body)
14692              article (gnus-group-real-name group) (nth 1 method))))
14693
14694 (defun gnus-request-post (method)
14695   "Post the current buffer using METHOD."
14696   (funcall (gnus-get-function method 'request-post) (nth 1 method)))
14697
14698 (defun gnus-request-scan (group method)
14699   "Request a SCAN being performed in GROUP from METHOD.
14700 If GROUP is nil, all groups on METHOD are scanned."
14701   (let ((method (if group (gnus-find-method-for-group group) method)))
14702     (funcall (gnus-get-function method 'request-scan)
14703              (and group (gnus-group-real-name group)) (nth 1 method))))
14704
14705 (defsubst gnus-request-update-info (info method)
14706   "Request that METHOD update INFO."
14707   (when (gnus-check-backend-function 'request-update-info (car method))
14708     (funcall (gnus-get-function method 'request-update-info)
14709              (gnus-group-real-name (gnus-info-group info))
14710              info (nth 1 method))))
14711
14712 (defun gnus-request-expire-articles (articles group &optional force)
14713   (let ((method (gnus-find-method-for-group group)))
14714     (funcall (gnus-get-function method 'request-expire-articles)
14715              articles (gnus-group-real-name group) (nth 1 method)
14716              force)))
14717
14718 (defun gnus-request-move-article
14719   (article group server accept-function &optional last)
14720   (let ((method (gnus-find-method-for-group group)))
14721     (funcall (gnus-get-function method 'request-move-article)
14722              article (gnus-group-real-name group)
14723              (nth 1 method) accept-function last)))
14724
14725 (defun gnus-request-accept-article (group &optional last method)
14726   ;; Make sure there's a newline at the end of the article.
14727   (goto-char (point-max))
14728   (unless (bolp)
14729     (insert "\n"))
14730   (let ((func (if (symbolp group) group
14731                 (car (or method (gnus-find-method-for-group group))))))
14732     (funcall (intern (format "%s-request-accept-article" func))
14733              (if (stringp group) (gnus-group-real-name group) group)
14734              last)))
14735
14736 (defun gnus-request-replace-article (article group buffer)
14737   (let ((func (car (gnus-find-method-for-group group))))
14738     (funcall (intern (format "%s-request-replace-article" func))
14739              article (gnus-group-real-name group) buffer)))
14740
14741 (defun gnus-request-associate-buffer (group)
14742   (let ((method (gnus-find-method-for-group group)))
14743     (funcall (gnus-get-function method 'request-associate-buffer)
14744              (gnus-group-real-name group))))
14745
14746 (defun gnus-request-restore-buffer (article group)
14747   "Request a new buffer restored to the state of ARTICLE."
14748   (let ((method (gnus-find-method-for-group group)))
14749     (funcall (gnus-get-function method 'request-restore-buffer)
14750              article (gnus-group-real-name group) (nth 1 method))))
14751
14752 (defun gnus-request-create-group (group &optional method)
14753   (let ((method (or method (gnus-find-method-for-group group))))
14754     (funcall (gnus-get-function method 'request-create-group)
14755              (gnus-group-real-name group) (nth 1 method))))
14756
14757 (defun gnus-request-delete-group (group &optional force)
14758   (let ((method (gnus-find-method-for-group group)))
14759     (funcall (gnus-get-function method 'request-delete-group)
14760              (gnus-group-real-name group) force (nth 1 method))))
14761
14762 (defun gnus-request-rename-group (group new-name)
14763   (let ((method (gnus-find-method-for-group group)))
14764     (funcall (gnus-get-function method 'request-rename-group)
14765              (gnus-group-real-name group)
14766              (gnus-group-real-name new-name) (nth 1 method))))
14767
14768 (defun gnus-member-of-valid (symbol group)
14769   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
14770   (memq symbol (assoc
14771                 (symbol-name (car (gnus-find-method-for-group group)))
14772                 gnus-valid-select-methods)))
14773
14774 (defun gnus-method-option-p (method option)
14775   "Return non-nil if select METHOD has OPTION as a parameter."
14776   (memq option (assoc (format "%s" (car method))
14777                       gnus-valid-select-methods)))
14778
14779 (defun gnus-server-extend-method (group method)
14780   ;; This function "extends" a virtual server.  If the server is
14781   ;; "hello", and the select method is ("hello" (my-var "something"))
14782   ;; in the group "alt.alt", this will result in a new virtual server
14783   ;; called "hello+alt.alt".
14784   (let ((entry
14785          (gnus-copy-sequence
14786           (if (equal (car method) "native") gnus-select-method
14787             (cdr (assoc (car method) gnus-server-alist))))))
14788     (setcar (cdr entry) (concat (nth 1 entry) "+" group))
14789     (nconc entry (cdr method))))
14790
14791 (defun gnus-find-method-for-group (group &optional info)
14792   "Find the select method that GROUP uses."
14793   (or gnus-override-method
14794       (and (not group)
14795            gnus-select-method)
14796       (let ((info (or info (gnus-get-info group)))
14797             method)
14798         (if (or (not info)
14799                 (not (setq method (gnus-info-method info)))
14800                 (equal method "native"))
14801             gnus-select-method
14802           (setq method
14803                 (cond ((stringp method)
14804                        (gnus-server-to-method method))
14805                       ((stringp (car method))
14806                        (gnus-server-extend-method group method))
14807                       (t
14808                        method)))
14809           (if (equal (cadr method) "")
14810               method
14811             (gnus-server-add-address method))))))
14812
14813 (defun gnus-check-backend-function (func group)
14814   "Check whether GROUP supports function FUNC."
14815   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
14816                   group)))
14817     (fboundp (intern (format "%s-%s" method func)))))
14818
14819 (defun gnus-methods-using (feature)
14820   "Find all methods that have FEATURE."
14821   (let ((valids gnus-valid-select-methods)
14822         outs)
14823     (while valids
14824       (if (memq feature (car valids))
14825           (setq outs (cons (car valids) outs)))
14826       (setq valids (cdr valids)))
14827     outs))
14828
14829 \f
14830 ;;;
14831 ;;; Active & Newsrc File Handling
14832 ;;;
14833
14834 (defun gnus-setup-news (&optional rawfile level)
14835   "Setup news information.
14836 If RAWFILE is non-nil, the .newsrc file will also be read.
14837 If LEVEL is non-nil, the news will be set up at level LEVEL."
14838   (let ((init (not (and gnus-newsrc-alist gnus-active-hashtb (not rawfile)))))
14839     ;; Clear some variables to re-initialize news information.
14840     (if init (setq gnus-newsrc-alist nil
14841                    gnus-active-hashtb nil))
14842
14843     ;; Read the newsrc file and create `gnus-newsrc-hashtb'.
14844     (if init (gnus-read-newsrc-file rawfile))
14845
14846     (when (and (not (assoc "archive" gnus-server-alist))
14847                gnus-message-archive-method)
14848       (push (cons "archive" gnus-message-archive-method)
14849             gnus-server-alist))
14850
14851     ;; If we don't read the complete active file, we fill in the
14852     ;; hashtb here.
14853     (if (or (null gnus-read-active-file)
14854             (eq gnus-read-active-file 'some))
14855         (gnus-update-active-hashtb-from-killed))
14856
14857     ;; Read the active file and create `gnus-active-hashtb'.
14858     ;; If `gnus-read-active-file' is nil, then we just create an empty
14859     ;; hash table.  The partial filling out of the hash table will be
14860     ;; done in `gnus-get-unread-articles'.
14861     (and gnus-read-active-file
14862          (not level)
14863          (gnus-read-active-file))
14864
14865     (or gnus-active-hashtb
14866         (setq gnus-active-hashtb (make-vector 4095 0)))
14867
14868     ;; Initialize the cache.
14869     (when gnus-use-cache
14870       (gnus-cache-open))
14871
14872     ;; Possibly eval the dribble file.
14873     (and init (or gnus-use-dribble-file gnus-slave) (gnus-dribble-eval-file))
14874
14875     (gnus-update-format-specifications)
14876
14877     ;; Find new newsgroups and treat them.
14878     (if (and init gnus-check-new-newsgroups (not level)
14879              (gnus-check-server gnus-select-method))
14880         (gnus-find-new-newsgroups))
14881
14882     ;; Find the number of unread articles in each non-dead group.
14883     (let ((gnus-read-active-file (and (not level) gnus-read-active-file)))
14884       (gnus-get-unread-articles level))
14885
14886     (if (and init gnus-check-bogus-newsgroups
14887              gnus-read-active-file (not level)
14888              (gnus-server-opened gnus-select-method))
14889         (gnus-check-bogus-newsgroups))))
14890
14891 (defun gnus-find-new-newsgroups (&optional arg)
14892   "Search for new newsgroups and add them.
14893 Each new newsgroup will be treated with `gnus-subscribe-newsgroup-method.'
14894 The `-n' option line from .newsrc is respected.
14895 If ARG (the prefix), use the `ask-server' method to query
14896 the server for new groups."
14897   (interactive "P")
14898   (let ((check (if (or (and arg (not (listp gnus-check-new-newsgroups)))
14899                        (null gnus-read-active-file)
14900                        (eq gnus-read-active-file 'some))
14901                    'ask-server gnus-check-new-newsgroups)))
14902     (unless (gnus-check-first-time-used)
14903       (if (or (consp check)
14904               (eq check 'ask-server))
14905           ;; Ask the server for new groups.
14906           (gnus-ask-server-for-new-groups)
14907         ;; Go through the active hashtb and look for new groups.
14908         (let ((groups 0)
14909               group new-newsgroups)
14910           (gnus-message 5 "Looking for new newsgroups...")
14911           (unless gnus-have-read-active-file
14912             (gnus-read-active-file))
14913           (setq gnus-newsrc-last-checked-date (current-time-string))
14914           (unless gnus-killed-hashtb
14915             (gnus-make-hashtable-from-killed))
14916           ;; Go though every newsgroup in `gnus-active-hashtb' and compare
14917           ;; with `gnus-newsrc-hashtb' and `gnus-killed-hashtb'.
14918           (mapatoms
14919            (lambda (sym)
14920              (if (or (null (setq group (symbol-name sym)))
14921                      (not (boundp sym))
14922                      (null (symbol-value sym))
14923                      (gnus-gethash group gnus-killed-hashtb)
14924                      (gnus-gethash group gnus-newsrc-hashtb))
14925                  ()
14926                (let ((do-sub (gnus-matches-options-n group)))
14927                  (cond
14928                   ((eq do-sub 'subscribe)
14929                    (setq groups (1+ groups))
14930                    (gnus-sethash group group gnus-killed-hashtb)
14931                    (funcall gnus-subscribe-options-newsgroup-method group))
14932                   ((eq do-sub 'ignore)
14933                    nil)
14934                   (t
14935                    (setq groups (1+ groups))
14936                    (gnus-sethash group group gnus-killed-hashtb)
14937                    (if gnus-subscribe-hierarchical-interactive
14938                        (setq new-newsgroups (cons group new-newsgroups))
14939                      (funcall gnus-subscribe-newsgroup-method group)))))))
14940            gnus-active-hashtb)
14941           (when new-newsgroups
14942             (gnus-subscribe-hierarchical-interactive new-newsgroups))
14943           ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
14944           (if (> groups 0)
14945               (gnus-message 6 "%d new newsgroup%s arrived."
14946                             groups (if (> groups 1) "s have" " has"))
14947             (gnus-message 6 "No new newsgroups.")))))))
14948
14949 (defun gnus-matches-options-n (group)
14950   ;; Returns `subscribe' if the group is to be unconditionally
14951   ;; subscribed, `ignore' if it is to be ignored, and nil if there is
14952   ;; no match for the group.
14953
14954   ;; First we check the two user variables.
14955   (cond
14956    ((and gnus-options-subscribe
14957          (string-match gnus-options-subscribe group))
14958     'subscribe)
14959    ((and gnus-auto-subscribed-groups
14960          (string-match gnus-auto-subscribed-groups group))
14961     'subscribe)
14962    ((and gnus-options-not-subscribe
14963          (string-match gnus-options-not-subscribe group))
14964     'ignore)
14965    ;; Then we go through the list that was retrieved from the .newsrc
14966    ;; file.  This list has elements on the form
14967    ;; `(REGEXP . {ignore,subscribe})'.  The first match found (the list
14968    ;; is in the reverse order of the options line) is returned.
14969    (t
14970     (let ((regs gnus-newsrc-options-n))
14971       (while (and regs
14972                   (not (string-match (caar regs) group)))
14973         (setq regs (cdr regs)))
14974       (and regs (cdar regs))))))
14975
14976 (defun gnus-ask-server-for-new-groups ()
14977   (let* ((date (or gnus-newsrc-last-checked-date (current-time-string)))
14978          (methods (cons gnus-select-method
14979                         (nconc
14980                          (when gnus-message-archive-method
14981                            (list "archive"))
14982                          (append
14983                           (and (consp gnus-check-new-newsgroups)
14984                                gnus-check-new-newsgroups)
14985                           gnus-secondary-select-methods))))
14986          (groups 0)
14987          (new-date (current-time-string))
14988          group new-newsgroups got-new method hashtb
14989          gnus-override-subscribe-method)
14990     ;; Go through both primary and secondary select methods and
14991     ;; request new newsgroups.
14992     (while (setq method (gnus-server-get-method nil (pop methods)))
14993       (setq new-newsgroups nil)
14994       (setq gnus-override-subscribe-method method)
14995       (when (and (gnus-check-server method)
14996                  (gnus-request-newgroups date method))
14997         (save-excursion
14998           (setq got-new t)
14999           (setq hashtb (gnus-make-hashtable 100))
15000           (set-buffer nntp-server-buffer)
15001           ;; Enter all the new groups into a hashtable.
15002           (gnus-active-to-gnus-format method hashtb 'ignore)))
15003       ;; Now all new groups from `method' are in `hashtb'.
15004       (mapatoms
15005        (lambda (group-sym)
15006          (if (or (null (setq group (symbol-name group-sym)))
15007                  (null (symbol-value group-sym))
15008                  (gnus-gethash group gnus-newsrc-hashtb)
15009                  (member group gnus-zombie-list)
15010                  (member group gnus-killed-list))
15011              ;; The group is already known.
15012              ()
15013            ;; Make this group active.
15014            (when (symbol-value group-sym)
15015              (gnus-set-active group (symbol-value group-sym)))
15016            ;; Check whether we want it or not.
15017            (let ((do-sub (gnus-matches-options-n group)))
15018              (cond
15019               ((eq do-sub 'subscribe)
15020                (incf groups)
15021                (gnus-sethash group group gnus-killed-hashtb)
15022                (funcall gnus-subscribe-options-newsgroup-method group))
15023               ((eq do-sub 'ignore)
15024                nil)
15025               (t
15026                (incf groups)
15027                (gnus-sethash group group gnus-killed-hashtb)
15028                (if gnus-subscribe-hierarchical-interactive
15029                    (push group new-newsgroups)
15030                  (funcall gnus-subscribe-newsgroup-method group)))))))
15031        hashtb)
15032       (when new-newsgroups
15033         (gnus-subscribe-hierarchical-interactive new-newsgroups)))
15034     ;; Suggested by Per Abrahamsen <amanda@iesd.auc.dk>.
15035     (when (> groups 0)
15036       (gnus-message 6 "%d new newsgroup%s arrived."
15037                     groups (if (> groups 1) "s have" " has")))
15038     (and got-new (setq gnus-newsrc-last-checked-date new-date))
15039     got-new))
15040
15041 (defun gnus-check-first-time-used ()
15042   (if (or (> (length gnus-newsrc-alist) 1)
15043           (file-exists-p gnus-startup-file)
15044           (file-exists-p (concat gnus-startup-file ".el"))
15045           (file-exists-p (concat gnus-startup-file ".eld")))
15046       nil
15047     (gnus-message 6 "First time user; subscribing you to default groups")
15048     (unless gnus-have-read-active-file
15049       (gnus-read-active-file))
15050     (setq gnus-newsrc-last-checked-date (current-time-string))
15051     (let ((groups gnus-default-subscribed-newsgroups)
15052           group)
15053       (if (eq groups t)
15054           nil
15055         (setq groups (or groups gnus-backup-default-subscribed-newsgroups))
15056         (mapatoms
15057          (lambda (sym)
15058            (if (null (setq group (symbol-name sym)))
15059                ()
15060              (let ((do-sub (gnus-matches-options-n group)))
15061                (cond
15062                 ((eq do-sub 'subscribe)
15063                  (gnus-sethash group group gnus-killed-hashtb)
15064                  (funcall gnus-subscribe-options-newsgroup-method group))
15065                 ((eq do-sub 'ignore)
15066                  nil)
15067                 (t
15068                  (setq gnus-killed-list (cons group gnus-killed-list)))))))
15069          gnus-active-hashtb)
15070         (while groups
15071           (if (gnus-active (car groups))
15072               (gnus-group-change-level
15073                (car groups) gnus-level-default-subscribed gnus-level-killed))
15074           (setq groups (cdr groups)))
15075         (gnus-group-make-help-group)
15076         (and gnus-novice-user
15077              (gnus-message 7 "`A k' to list killed groups"))))))
15078
15079 (defun gnus-subscribe-group (group previous &optional method)
15080   (gnus-group-change-level
15081    (if method
15082        (list t group gnus-level-default-subscribed nil nil method)
15083      group)
15084    gnus-level-default-subscribed gnus-level-killed previous t))
15085
15086 ;; `gnus-group-change-level' is the fundamental function for changing
15087 ;; subscription levels of newsgroups.  This might mean just changing
15088 ;; from level 1 to 2, which is pretty trivial, from 2 to 6 or back
15089 ;; again, which subscribes/unsubscribes a group, which is equally
15090 ;; trivial.  Changing from 1-7 to 8-9 means that you kill a group, and
15091 ;; from 8-9 to 1-7 means that you remove the group from the list of
15092 ;; killed (or zombie) groups and add them to the (kinda) subscribed
15093 ;; groups.  And last but not least, moving from 8 to 9 and 9 to 8,
15094 ;; which is trivial.
15095 ;; ENTRY can either be a string (newsgroup name) or a list (if
15096 ;; FROMKILLED is t, it's a list on the format (NUM INFO-LIST),
15097 ;; otherwise it's a list in the format of the `gnus-newsrc-hashtb'
15098 ;; entries.
15099 ;; LEVEL is the new level of the group, OLDLEVEL is the old level and
15100 ;; PREVIOUS is the group (in hashtb entry format) to insert this group
15101 ;; after.
15102 (defun gnus-group-change-level (entry level &optional oldlevel
15103                                       previous fromkilled)
15104   (let (group info active num)
15105     ;; Glean what info we can from the arguments
15106     (if (consp entry)
15107         (if fromkilled (setq group (nth 1 entry))
15108           (setq group (car (nth 2 entry))))
15109       (setq group entry))
15110     (if (and (stringp entry)
15111              oldlevel
15112              (< oldlevel gnus-level-zombie))
15113         (setq entry (gnus-gethash entry gnus-newsrc-hashtb)))
15114     (if (and (not oldlevel)
15115              (consp entry))
15116         (setq oldlevel (gnus-info-level (nth 2 entry)))
15117       (setq oldlevel 9))
15118     (if (stringp previous)
15119         (setq previous (gnus-gethash previous gnus-newsrc-hashtb)))
15120
15121     (if (and (>= oldlevel gnus-level-zombie)
15122              (gnus-gethash group gnus-newsrc-hashtb))
15123         ;; We are trying to subscribe a group that is already
15124         ;; subscribed.
15125         ()                              ; Do nothing.
15126
15127       (or (gnus-ephemeral-group-p group)
15128           (gnus-dribble-enter
15129            (format "(gnus-group-change-level %S %S %S %S %S)"
15130                    group level oldlevel (car (nth 2 previous)) fromkilled)))
15131
15132       ;; Then we remove the newgroup from any old structures, if needed.
15133       ;; If the group was killed, we remove it from the killed or zombie
15134       ;; list.  If not, and it is in fact going to be killed, we remove
15135       ;; it from the newsrc hash table and assoc.
15136       (cond
15137        ((>= oldlevel gnus-level-zombie)
15138         (if (= oldlevel gnus-level-zombie)
15139             (setq gnus-zombie-list (delete group gnus-zombie-list))
15140           (setq gnus-killed-list (delete group gnus-killed-list))))
15141        (t
15142         (if (and (>= level gnus-level-zombie)
15143                  entry)
15144             (progn
15145               (gnus-sethash (car (nth 2 entry)) nil gnus-newsrc-hashtb)
15146               (if (nth 3 entry)
15147                   (setcdr (gnus-gethash (car (nth 3 entry))
15148                                         gnus-newsrc-hashtb)
15149                           (cdr entry)))
15150               (setcdr (cdr entry) (cdddr entry))))))
15151
15152       ;; Finally we enter (if needed) the list where it is supposed to
15153       ;; go, and change the subscription level.  If it is to be killed,
15154       ;; we enter it into the killed or zombie list.
15155       (cond 
15156        ((>= level gnus-level-zombie)
15157         ;; Remove from the hash table.
15158         (gnus-sethash group nil gnus-newsrc-hashtb)
15159         ;; We do not enter foreign groups into the list of dead
15160         ;; groups.
15161         (unless (gnus-group-foreign-p group)
15162           (if (= level gnus-level-zombie)
15163               (setq gnus-zombie-list (cons group gnus-zombie-list))
15164             (setq gnus-killed-list (cons group gnus-killed-list)))))
15165        (t
15166         ;; If the list is to be entered into the newsrc assoc, and
15167         ;; it was killed, we have to create an entry in the newsrc
15168         ;; hashtb format and fix the pointers in the newsrc assoc.
15169         (if (< oldlevel gnus-level-zombie)
15170             ;; It was alive, and it is going to stay alive, so we
15171             ;; just change the level and don't change any pointers or
15172             ;; hash table entries.
15173             (setcar (cdaddr entry) level)
15174           (if (listp entry)
15175               (setq info (cdr entry)
15176                     num (car entry))
15177             (setq active (gnus-active group))
15178             (setq num
15179                   (if active (- (1+ (cdr active)) (car active)) t))
15180             ;; Check whether the group is foreign.  If so, the
15181             ;; foreign select method has to be entered into the
15182             ;; info.
15183             (let ((method (or gnus-override-subscribe-method
15184                               (gnus-group-method group))))
15185               (if (eq method gnus-select-method)
15186                   (setq info (list group level nil))
15187                 (setq info (list group level nil nil method)))))
15188           (unless previous
15189             (setq previous
15190                   (let ((p gnus-newsrc-alist))
15191                     (while (cddr p)
15192                       (setq p (cdr p)))
15193                     p)))
15194           (setq entry (cons info (cddr previous)))
15195           (if (cdr previous)
15196               (progn
15197                 (setcdr (cdr previous) entry)
15198                 (gnus-sethash group (cons num (cdr previous))
15199                               gnus-newsrc-hashtb))
15200             (setcdr previous entry)
15201             (gnus-sethash group (cons num previous)
15202                           gnus-newsrc-hashtb))
15203           (when (cdr entry)
15204             (setcdr (gnus-gethash (caadr entry) gnus-newsrc-hashtb) entry)))))
15205       (when gnus-group-change-level-function
15206         (funcall gnus-group-change-level-function group level oldlevel)))))
15207
15208 (defun gnus-kill-newsgroup (newsgroup)
15209   "Obsolete function.  Kills a newsgroup."
15210   (gnus-group-change-level
15211    (gnus-gethash newsgroup gnus-newsrc-hashtb) gnus-level-killed))
15212
15213 (defun gnus-check-bogus-newsgroups (&optional confirm)
15214   "Remove bogus newsgroups.
15215 If CONFIRM is non-nil, the user has to confirm the deletion of every
15216 newsgroup."
15217   (let ((newsrc (cdr gnus-newsrc-alist))
15218         bogus group entry info)
15219     (gnus-message 5 "Checking bogus newsgroups...")
15220     (unless gnus-have-read-active-file
15221       (gnus-read-active-file))
15222     (when (member gnus-select-method gnus-have-read-active-file)
15223       ;; Find all bogus newsgroup that are subscribed.
15224       (while newsrc
15225         (setq info (pop newsrc)
15226               group (gnus-info-group info))
15227         (unless (or (gnus-active group) ; Active
15228                     (gnus-info-method info) ; Foreign
15229                     (and confirm
15230                          (not (gnus-y-or-n-p
15231                                (format "Remove bogus newsgroup: %s " group)))))
15232           ;; Found a bogus newsgroup.
15233           (push group bogus)))
15234       ;; Remove all bogus subscribed groups by first killing them, and
15235       ;; then removing them from the list of killed groups.
15236       (while bogus
15237         (when (setq entry (gnus-gethash (setq group (pop bogus))
15238                                         gnus-newsrc-hashtb))
15239           (gnus-group-change-level entry gnus-level-killed)
15240           (setq gnus-killed-list (delete group gnus-killed-list))))
15241       ;; Then we remove all bogus groups from the list of killed and
15242       ;; zombie groups.  They are are removed without confirmation.
15243       (let ((dead-lists '(gnus-killed-list gnus-zombie-list))
15244             killed)
15245         (while dead-lists
15246           (setq killed (symbol-value (car dead-lists)))
15247           (while killed
15248             (unless (gnus-active (setq group (pop killed)))
15249               ;; The group is bogus.
15250               ;; !!!Slow as hell.
15251               (set (car dead-lists)
15252                    (delete group (symbol-value (car dead-lists))))))
15253           (setq dead-lists (cdr dead-lists))))
15254       (gnus-message 5 "Checking bogus newsgroups...done"))))
15255
15256 (defun gnus-check-duplicate-killed-groups ()
15257   "Remove duplicates from the list of killed groups."
15258   (interactive)
15259   (let ((killed gnus-killed-list))
15260     (while killed
15261       (gnus-message 9 "%d" (length killed))
15262       (setcdr killed (delete (car killed) (cdr killed)))
15263       (setq killed (cdr killed)))))
15264
15265 ;; Go though `gnus-newsrc-alist' and compare with `gnus-active-hashtb'
15266 ;; and compute how many unread articles there are in each group.
15267 (defun gnus-get-unread-articles (&optional level)
15268   (let* ((newsrc (cdr gnus-newsrc-alist))
15269          (level (or level gnus-activate-level (1+ gnus-level-subscribed)))
15270          (foreign-level
15271           (min
15272            (cond ((and gnus-activate-foreign-newsgroups
15273                        (not (numberp gnus-activate-foreign-newsgroups)))
15274                   (1+ gnus-level-subscribed))
15275                  ((numberp gnus-activate-foreign-newsgroups)
15276                   gnus-activate-foreign-newsgroups)
15277                  (t 0))
15278            level))
15279          info group active method)
15280     (gnus-message 5 "Checking new news...")
15281
15282     (while newsrc
15283       (setq active (gnus-active (setq group (gnus-info-group
15284                                              (setq info (pop newsrc))))))
15285
15286       ;; Check newsgroups.  If the user doesn't want to check them, or
15287       ;; they can't be checked (for instance, if the news server can't
15288       ;; be reached) we just set the number of unread articles in this
15289       ;; newsgroup to t.  This means that Gnus thinks that there are
15290       ;; unread articles, but it has no idea how many.
15291       (if (and (setq method (gnus-info-method info))
15292                (not (gnus-server-equal
15293                      gnus-select-method
15294                      (gnus-server-get-method nil method)))
15295                (not (gnus-secondary-method-p method)))
15296           ;; These groups are foreign.  Check the level.
15297           (when (<= (gnus-info-level info) foreign-level)
15298             (setq active (gnus-activate-group group 'scan))
15299             (gnus-close-group group))
15300
15301         ;; These groups are native or secondary.
15302         (when (and (<= (gnus-info-level info) level)
15303                    (not gnus-read-active-file))
15304           (setq active (gnus-activate-group group 'scan))
15305           (gnus-close-group group)))
15306
15307       (if active
15308           (gnus-get-unread-articles-in-group info active t)
15309         ;; The group couldn't be reached, so we nix out the number of
15310         ;; unread articles and stuff.
15311         (gnus-set-active group nil)
15312         (setcar (gnus-gethash group gnus-newsrc-hashtb) t)))
15313
15314     (gnus-message 5 "Checking new news...done")))
15315
15316 ;; Create a hash table out of the newsrc alist.  The `car's of the
15317 ;; alist elements are used as keys.
15318 (defun gnus-make-hashtable-from-newsrc-alist ()
15319   (let ((alist gnus-newsrc-alist)
15320         (ohashtb gnus-newsrc-hashtb)
15321         prev)
15322     (setq gnus-newsrc-hashtb (gnus-make-hashtable (length alist)))
15323     (setq alist
15324           (setq prev (setq gnus-newsrc-alist
15325                            (if (equal (caar gnus-newsrc-alist)
15326                                       "dummy.group")
15327                                gnus-newsrc-alist
15328                              (cons (list "dummy.group" 0 nil) alist)))))
15329     (while alist
15330       (gnus-sethash
15331        (caar alist)
15332        (cons (and ohashtb (car (gnus-gethash (caar alist) ohashtb)))
15333              prev)
15334        gnus-newsrc-hashtb)
15335       (setq prev alist
15336             alist (cdr alist)))))
15337
15338 (defun gnus-make-hashtable-from-killed ()
15339   "Create a hash table from the killed and zombie lists."
15340   (let ((lists '(gnus-killed-list gnus-zombie-list))
15341         list)
15342     (setq gnus-killed-hashtb
15343           (gnus-make-hashtable
15344            (+ (length gnus-killed-list) (length gnus-zombie-list))))
15345     (while lists
15346       (setq list (symbol-value (car lists)))
15347       (setq lists (cdr lists))
15348       (while list
15349         (gnus-sethash (car list) (car list) gnus-killed-hashtb)
15350         (setq list (cdr list))))))
15351
15352 (defun gnus-get-unread-articles-in-group (info active &optional update)
15353   (when active
15354     ;; Allow the backend to update the info in the group.
15355     (when (and update 
15356                (gnus-request-update-info
15357                 info (gnus-find-method-for-group (gnus-info-group info))))
15358       (gnus-activate-group (gnus-info-group info)))
15359     (let* ((range (gnus-info-read info))
15360            (num 0)
15361            (marked (gnus-info-marks info)))
15362       ;; If a cache is present, we may have to alter the active info.
15363       (when (and gnus-use-cache info)
15364         (gnus-cache-possibly-alter-active (gnus-info-group info) active))
15365       ;; Modify the list of read articles according to what articles
15366       ;; are available; then tally the unread articles and add the
15367       ;; number to the group hash table entry.
15368       (cond
15369        ((zerop (cdr active))
15370         (setq num 0))
15371        ((not range)
15372         (setq num (- (1+ (cdr active)) (car active))))
15373        ((not (listp (cdr range)))
15374         ;; Fix a single (num . num) range according to the
15375         ;; active hash table.
15376         ;; Fix by Carsten Bormann <cabo@Informatik.Uni-Bremen.DE>.
15377         (and (< (cdr range) (car active)) (setcdr range (1- (car active))))
15378         (and (> (cdr range) (cdr active)) (setcdr range (cdr active)))
15379         ;; Compute number of unread articles.
15380         (setq num (max 0 (- (cdr active) (- (1+ (cdr range)) (car range))))))
15381        (t
15382         ;; The read list is a list of ranges.  Fix them according to
15383         ;; the active hash table.
15384         ;; First peel off any elements that are below the lower
15385         ;; active limit.
15386         (while (and (cdr range)
15387                     (>= (car active)
15388                         (or (and (atom (cadr range)) (cadr range))
15389                             (caadr range))))
15390           (if (numberp (car range))
15391               (setcar range
15392                       (cons (car range)
15393                             (or (and (numberp (cadr range))
15394                                      (cadr range))
15395                                 (cdadr range))))
15396             (setcdr (car range)
15397                     (or (and (numberp (nth 1 range)) (nth 1 range))
15398                         (cdadr range))))
15399           (setcdr range (cddr range)))
15400         ;; Adjust the first element to be the same as the lower limit.
15401         (if (and (not (atom (car range)))
15402                  (< (cdar range) (car active)))
15403             (setcdr (car range) (1- (car active))))
15404         ;; Then we want to peel off any elements that are higher
15405         ;; than the upper active limit.
15406         (let ((srange range))
15407           ;; Go past all legal elements.
15408           (while (and (cdr srange)
15409                       (<= (or (and (atom (cadr srange))
15410                                    (cadr srange))
15411                               (caadr srange)) (cdr active)))
15412             (setq srange (cdr srange)))
15413           (if (cdr srange)
15414               ;; Nuke all remaining illegal elements.
15415               (setcdr srange nil))
15416
15417           ;; Adjust the final element.
15418           (if (and (not (atom (car srange)))
15419                    (> (cdar srange) (cdr active)))
15420               (setcdr (car srange) (cdr active))))
15421         ;; Compute the number of unread articles.
15422         (while range
15423           (setq num (+ num (- (1+ (or (and (atom (car range)) (car range))
15424                                       (cdar range)))
15425                               (or (and (atom (car range)) (car range))
15426                                   (caar range)))))
15427           (setq range (cdr range)))
15428         (setq num (max 0 (- (cdr active) num)))))
15429       ;; Set the number of unread articles.
15430       (when info
15431         (setcar (gnus-gethash (gnus-info-group info) gnus-newsrc-hashtb) num))
15432       num)))
15433
15434 (defun gnus-activate-group (group &optional scan)
15435   ;; Check whether a group has been activated or not.
15436   ;; If SCAN, request a scan of that group as well.
15437   (let ((method (gnus-find-method-for-group group))
15438         active)
15439     (and (gnus-check-server method)
15440          ;; We escape all bugs and quit here to make it possible to
15441          ;; continue if a group is so out-there that it reports bugs
15442          ;; and stuff.
15443          (progn
15444            (and scan
15445                 (gnus-check-backend-function 'request-scan (car method))
15446                 (gnus-request-scan group method))
15447            t)
15448          (condition-case ()
15449              (gnus-request-group group)
15450         ;   (error nil)
15451            (quit nil))
15452          (save-excursion
15453            (set-buffer nntp-server-buffer)
15454            (goto-char (point-min))
15455            ;; Parse the result we got from `gnus-request-group'.
15456            (and (looking-at "[0-9]+ [0-9]+ \\([0-9]+\\) [0-9]+")
15457                 (progn
15458                   (goto-char (match-beginning 1))
15459                   (gnus-set-active
15460                    group (setq active (cons (read (current-buffer))
15461                                             (read (current-buffer)))))
15462                   ;; Return the new active info.
15463                   active))))))
15464
15465 (defun gnus-update-read-articles (group unread)
15466   "Update the list of read and ticked articles in GROUP using the
15467 UNREAD and TICKED lists.
15468 Note: UNSELECTED has to be sorted over `<'.
15469 Returns whether the updating was successful."
15470   (let* ((active (or gnus-newsgroup-active (gnus-active group)))
15471          (entry (gnus-gethash group gnus-newsrc-hashtb))
15472          (info (nth 2 entry))
15473          (marked (gnus-info-marks info))
15474          (prev 1)
15475          (unread (sort (copy-sequence unread) '<))
15476          read)
15477     (if (or (not info) (not active))
15478         ;; There is no info on this group if it was, in fact,
15479         ;; killed.  Gnus stores no information on killed groups, so
15480         ;; there's nothing to be done.
15481         ;; One could store the information somewhere temporarily,
15482         ;; perhaps...  Hmmm...
15483         ()
15484       ;; Remove any negative articles numbers.
15485       (while (and unread (< (car unread) 0))
15486         (setq unread (cdr unread)))
15487       ;; Remove any expired article numbers
15488       (while (and unread (< (car unread) (car active)))
15489         (setq unread (cdr unread)))
15490       ;; Compute the ranges of read articles by looking at the list of
15491       ;; unread articles.
15492       (while unread
15493         (if (/= (car unread) prev)
15494             (setq read (cons (if (= prev (1- (car unread))) prev
15495                                (cons prev (1- (car unread)))) read)))
15496         (setq prev (1+ (car unread)))
15497         (setq unread (cdr unread)))
15498       (when (<= prev (cdr active))
15499         (setq read (cons (cons prev (cdr active)) read)))
15500       ;; Enter this list into the group info.
15501       (gnus-info-set-read
15502        info (if (> (length read) 1) (nreverse read) read))
15503       ;; Set the number of unread articles in gnus-newsrc-hashtb.
15504       (gnus-get-unread-articles-in-group info (gnus-active group))
15505       t)))
15506
15507 (defun gnus-make-articles-unread (group articles)
15508   "Mark ARTICLES in GROUP as unread."
15509   (let* ((info (nth 2 (or (gnus-gethash group gnus-newsrc-hashtb)
15510                           (gnus-gethash (gnus-group-real-name group)
15511                                         gnus-newsrc-hashtb))))
15512          (ranges (gnus-info-read info))
15513          news article)
15514     (while articles
15515       (when (gnus-member-of-range
15516              (setq article (pop articles)) ranges)
15517         (setq news (cons article news))))
15518     (when news
15519       (gnus-info-set-read
15520        info (gnus-remove-from-range (gnus-info-read info) (nreverse news)))
15521       (gnus-group-update-group group t))))
15522
15523 ;; Enter all dead groups into the hashtb.
15524 (defun gnus-update-active-hashtb-from-killed ()
15525   (let ((hashtb (setq gnus-active-hashtb (make-vector 4095 0)))
15526         (lists (list gnus-killed-list gnus-zombie-list))
15527         killed)
15528     (while lists
15529       (setq killed (car lists))
15530       (while killed
15531         (gnus-sethash (car killed) nil hashtb)
15532         (setq killed (cdr killed)))
15533       (setq lists (cdr lists)))))
15534
15535 ;; Get the active file(s) from the backend(s).
15536 (defun gnus-read-active-file ()
15537   (gnus-group-set-mode-line)
15538   (let ((methods 
15539          (append
15540           (if (gnus-check-server gnus-select-method)
15541               ;; The native server is available.
15542               (cons gnus-select-method gnus-secondary-select-methods)
15543             ;; The native server is down, so we just do the
15544             ;; secondary ones.
15545             gnus-secondary-select-methods)
15546           ;; Also read from the archive server.
15547           (when gnus-message-archive-method
15548             (list "archive"))))
15549         list-type)
15550     (setq gnus-have-read-active-file nil)
15551     (save-excursion
15552       (set-buffer nntp-server-buffer)
15553       (while methods
15554         (let* ((method (if (stringp (car methods))
15555                            (gnus-server-get-method nil (car methods))
15556                          (car methods)))
15557                (where (nth 1 method))
15558                (mesg (format "Reading active file%s via %s..."
15559                              (if (and where (not (zerop (length where))))
15560                                  (concat " from " where) "")
15561                              (car method))))
15562           (gnus-message 5 mesg)
15563           (when (gnus-check-server method)
15564             ;; Request that the backend scan its incoming messages.
15565             (and (gnus-check-backend-function 'request-scan (car method))
15566                  (gnus-request-scan nil method))
15567             (cond
15568              ((and (eq gnus-read-active-file 'some)
15569                    (gnus-check-backend-function 'retrieve-groups (car method)))
15570               (let ((newsrc (cdr gnus-newsrc-alist))
15571                     (gmethod (if (stringp method)
15572                                  (gnus-server-get-method nil method)
15573                                method))
15574                     groups info)
15575                 (while (setq info (pop newsrc))
15576                   (when (gnus-server-equal
15577                          (gnus-find-method-for-group 
15578                           (gnus-info-group info) info)
15579                          gmethod)
15580                     (push (gnus-group-real-name (gnus-info-group info)) 
15581                           groups)))
15582                 (when groups
15583                   (gnus-check-server method)
15584                   (setq list-type (gnus-retrieve-groups groups method))
15585                   (cond
15586                    ((not list-type)
15587                     (gnus-message
15588                      1 "Cannot read partial active file from %s server."
15589                      (car method))
15590                     (ding)
15591                     (sit-for 2))
15592                    ((eq list-type 'active)
15593                     (gnus-active-to-gnus-format method gnus-active-hashtb))
15594                    (t
15595                     (gnus-groups-to-gnus-format method gnus-active-hashtb))))))
15596              (t
15597               (if (not (gnus-request-list method))
15598                   (progn
15599                     (unless (equal method gnus-message-archive-method)
15600                       (gnus-message 1 "Cannot read active file from %s server."
15601                                     (car method))
15602                       (ding)))
15603                 (gnus-active-to-gnus-format method)
15604                 ;; We mark this active file as read.
15605                 (push method gnus-have-read-active-file)
15606                 (gnus-message 5 "%sdone" mesg))))))
15607         (setq methods (cdr methods))))))
15608
15609 ;; Read an active file and place the results in `gnus-active-hashtb'.
15610 (defun gnus-active-to-gnus-format (&optional method hashtb ignore-errors)
15611   (unless method
15612     (setq method gnus-select-method))
15613   (let ((cur (current-buffer))
15614         (hashtb (or hashtb
15615                     (if (and gnus-active-hashtb
15616                              (not (equal method gnus-select-method)))
15617                         gnus-active-hashtb
15618                       (setq gnus-active-hashtb
15619                             (if (equal method gnus-select-method)
15620                                 (gnus-make-hashtable
15621                                  (count-lines (point-min) (point-max)))
15622                               (gnus-make-hashtable 4096))))))
15623         (flag-hashtb (gnus-make-hashtable 60)))
15624     ;; Delete unnecessary lines.
15625     (goto-char (point-min))
15626     (while (search-forward "\nto." nil t)
15627       (delete-region (1+ (match-beginning 0))
15628                      (progn (forward-line 1) (point))))
15629     (or (string= gnus-ignored-newsgroups "")
15630         (progn
15631           (goto-char (point-min))
15632           (delete-matching-lines gnus-ignored-newsgroups)))
15633     ;; Make the group names readable as a lisp expression even if they
15634     ;; contain special characters.
15635     ;; Fix by Luc Van Eycken <Luc.VanEycken@esat.kuleuven.ac.be>.
15636     (goto-char (point-max))
15637     (while (re-search-backward "[][';?()#]" nil t)
15638       (insert ?\\))
15639     ;; If these are groups from a foreign select method, we insert the
15640     ;; group prefix in front of the group names.
15641     (and method (not (gnus-server-equal
15642                       (gnus-server-get-method nil method)
15643                       (gnus-server-get-method nil gnus-select-method)))
15644          (let ((prefix (gnus-group-prefixed-name "" method)))
15645            (goto-char (point-min))
15646            (while (and (not (eobp))
15647                        (progn (insert prefix)
15648                               (zerop (forward-line 1)))))))
15649     ;; Store the active file in a hash table.
15650     (goto-char (point-min))
15651     (if (string-match "%[oO]" gnus-group-line-format)
15652         ;; Suggested by Brian Edmonds <edmonds@cs.ubc.ca>.
15653         ;; If we want information on moderated groups, we use this
15654         ;; loop...
15655         (let* ((mod-hashtb (make-vector 7 0))
15656                (m (intern "m" mod-hashtb))
15657                group max min)
15658           (while (not (eobp))
15659             (condition-case nil
15660                 (progn
15661                   (narrow-to-region (point) (gnus-point-at-eol))
15662                   (setq group (let ((obarray hashtb)) (read cur)))
15663                   (if (and (numberp (setq max (read cur)))
15664                            (numberp (setq min (read cur)))
15665                            (progn
15666                              (skip-chars-forward " \t")
15667                              (not
15668                               (or (= (following-char) ?=)
15669                                   (= (following-char) ?x)
15670                                   (= (following-char) ?j)))))
15671                       (set group (cons min max))
15672                     (set group nil))
15673                   ;; Enter moderated groups into a list.
15674                   (if (eq (let ((obarray mod-hashtb)) (read cur)) m)
15675                       (setq gnus-moderated-list
15676                             (cons (symbol-name group) gnus-moderated-list))))
15677               (error
15678                (and group
15679                     (symbolp group)
15680                     (set group nil))))
15681             (widen)
15682             (forward-line 1)))
15683       ;; And if we do not care about moderation, we use this loop,
15684       ;; which is faster.
15685       (let (group max min)
15686         (while (not (eobp))
15687           (condition-case ()
15688               (progn
15689                 (narrow-to-region (point) (gnus-point-at-eol))
15690                 ;; group gets set to a symbol interned in the hash table
15691                 ;; (what a hack!!) - jwz
15692                 (setq group (let ((obarray hashtb)) (read cur)))
15693                 (if (and (numberp (setq max (read cur)))
15694                          (numberp (setq min (read cur)))
15695                          (progn
15696                            (skip-chars-forward " \t")
15697                            (not
15698                             (or (= (following-char) ?=)
15699                                 (= (following-char) ?x)
15700                                 (= (following-char) ?j)))))
15701                     (set group (cons min max))
15702                   (set group nil)))
15703             (error
15704              (progn
15705                (and group
15706                     (symbolp group)
15707                     (set group nil))
15708                (or ignore-errors
15709                    (gnus-message 3 "Warning - illegal active: %s"
15710                                  (buffer-substring
15711                                   (gnus-point-at-bol) (gnus-point-at-eol)))))))
15712           (widen)
15713           (forward-line 1))))))
15714
15715 (defun gnus-groups-to-gnus-format (method &optional hashtb)
15716   ;; Parse a "groups" active file.
15717   (let ((cur (current-buffer))
15718         (hashtb (or hashtb
15719                     (if (and method gnus-active-hashtb)
15720                         gnus-active-hashtb
15721                       (setq gnus-active-hashtb
15722                             (gnus-make-hashtable
15723                              (count-lines (point-min) (point-max)))))))
15724         (prefix (and method
15725                      (not (gnus-server-equal
15726                            (gnus-server-get-method nil method)
15727                            (gnus-server-get-method nil gnus-select-method)))
15728                      (gnus-group-prefixed-name "" method))))
15729
15730     (goto-char (point-min))
15731     ;; We split this into to separate loops, one with the prefix
15732     ;; and one without to speed the reading up somewhat.
15733     (if prefix
15734         (let (min max opoint group)
15735           (while (not (eobp))
15736             (condition-case ()
15737                 (progn
15738                   (read cur) (read cur)
15739                   (setq min (read cur)
15740                         max (read cur)
15741                         opoint (point))
15742                   (skip-chars-forward " \t")
15743                   (insert prefix)
15744                   (goto-char opoint)
15745                   (set (let ((obarray hashtb)) (read cur))
15746                        (cons min max)))
15747               (error (and group (symbolp group) (set group nil))))
15748             (forward-line 1)))
15749       (let (min max group)
15750         (while (not (eobp))
15751           (condition-case ()
15752               (if (= (following-char) ?2)
15753                   (progn
15754                     (read cur) (read cur)
15755                     (setq min (read cur)
15756                           max (read cur))
15757                     (set (setq group (let ((obarray hashtb)) (read cur)))
15758                          (cons min max))))
15759             (error (and group (symbolp group) (set group nil))))
15760           (forward-line 1))))))
15761
15762 (defun gnus-read-newsrc-file (&optional force)
15763   "Read startup file.
15764 If FORCE is non-nil, the .newsrc file is read."
15765   ;; Reset variables that might be defined in the .newsrc.eld file.
15766   (let ((variables gnus-variable-list))
15767     (while variables
15768       (set (car variables) nil)
15769       (setq variables (cdr variables))))
15770   (let* ((newsrc-file gnus-current-startup-file)
15771          (quick-file (concat newsrc-file ".el")))
15772     (save-excursion
15773       ;; We always load the .newsrc.eld file.  If always contains
15774       ;; much information that can not be gotten from the .newsrc
15775       ;; file (ticked articles, killed groups, foreign methods, etc.)
15776       (gnus-read-newsrc-el-file quick-file)
15777
15778       (if (and (file-exists-p gnus-current-startup-file)
15779                (or force
15780                    (and (file-newer-than-file-p newsrc-file quick-file)
15781                         (file-newer-than-file-p newsrc-file
15782                                                 (concat quick-file "d")))
15783                    (not gnus-newsrc-alist)))
15784           ;; We read the .newsrc file.  Note that if there if a
15785           ;; .newsrc.eld file exists, it has already been read, and
15786           ;; the `gnus-newsrc-hashtb' has been created.  While reading
15787           ;; the .newsrc file, Gnus will only use the information it
15788           ;; can find there for changing the data already read -
15789           ;; ie. reading the .newsrc file will not trash the data
15790           ;; already read (except for read articles).
15791           (save-excursion
15792             (gnus-message 5 "Reading %s..." newsrc-file)
15793             (set-buffer (find-file-noselect newsrc-file))
15794             (buffer-disable-undo (current-buffer))
15795             (gnus-newsrc-to-gnus-format)
15796             (kill-buffer (current-buffer))
15797             (gnus-message 5 "Reading %s...done" newsrc-file)))
15798
15799       ;; Read any slave files.
15800       (or gnus-slave
15801           (gnus-master-read-slave-newsrc)))))
15802
15803 (defun gnus-read-newsrc-el-file (file)
15804   (let ((ding-file (concat file "d")))
15805     ;; We always, always read the .eld file.
15806     (gnus-message 5 "Reading %s..." ding-file)
15807     (let (gnus-newsrc-assoc)
15808       (condition-case nil
15809           (load ding-file t t t)
15810         (error
15811          (gnus-message 1 "Error in %s" ding-file)
15812          (ding)))
15813       (when gnus-newsrc-assoc
15814         (setq gnus-newsrc-alist gnus-newsrc-assoc)))
15815     (gnus-make-hashtable-from-newsrc-alist)
15816     (when (file-newer-than-file-p file ding-file)
15817       ;; Old format quick file
15818       (gnus-message 5 "Reading %s..." file)
15819       ;; The .el file is newer than the .eld file, so we read that one
15820       ;; as well.
15821       (gnus-read-old-newsrc-el-file file))))
15822
15823 ;; Parse the old-style quick startup file
15824 (defun gnus-read-old-newsrc-el-file (file)
15825   (let (newsrc killed marked group m)
15826     (prog1
15827         (let ((gnus-killed-assoc nil)
15828               gnus-marked-assoc gnus-newsrc-alist gnus-newsrc-assoc)
15829           (prog1
15830               (condition-case nil
15831                   (load file t t t)
15832                 (error nil))
15833             (setq newsrc gnus-newsrc-assoc
15834                   killed gnus-killed-assoc
15835                   marked gnus-marked-assoc)))
15836       (setq gnus-newsrc-alist nil)
15837       (while newsrc
15838         (setq group (car newsrc))
15839         (let ((info (gnus-get-info (car group))))
15840           (if info
15841               (progn
15842                 (gnus-info-set-read info (cddr group))
15843                 (gnus-info-set-level
15844                  info (if (nth 1 group) gnus-level-default-subscribed
15845                         gnus-level-default-unsubscribed))
15846                 (setq gnus-newsrc-alist (cons info gnus-newsrc-alist)))
15847             (setq gnus-newsrc-alist
15848                   (cons
15849                    (setq info
15850                          (list (car group)
15851                                (if (nth 1 group) gnus-level-default-subscribed
15852                                  gnus-level-default-unsubscribed)
15853                                (cddr group)))
15854                    gnus-newsrc-alist)))
15855           (if (setq m (assoc (car group) marked))
15856               (gnus-info-set-marks
15857                info (cons (list (cons 'tick (gnus-compress-sequence
15858                                              (sort (cdr m) '<) t)))
15859                           nil))))
15860         (setq newsrc (cdr newsrc)))
15861       (setq newsrc killed)
15862       (while newsrc
15863         (setcar newsrc (caar newsrc))
15864         (setq newsrc (cdr newsrc)))
15865       (setq gnus-killed-list killed))
15866     ;; The .el file version of this variable does not begin with
15867     ;; "options", while the .eld version does, so we just add it if it
15868     ;; isn't there.
15869     (and
15870      gnus-newsrc-options
15871      (progn
15872        (and (not (string-match "^ *options" gnus-newsrc-options))
15873             (setq gnus-newsrc-options (concat "options " gnus-newsrc-options)))
15874        (and (not (string-match "\n$" gnus-newsrc-options))
15875             (setq gnus-newsrc-options (concat gnus-newsrc-options "\n")))
15876        ;; Finally, if we read some options lines, we parse them.
15877        (or (string= gnus-newsrc-options "")
15878            (gnus-newsrc-parse-options gnus-newsrc-options))))
15879
15880     (setq gnus-newsrc-alist (nreverse gnus-newsrc-alist))
15881     (gnus-make-hashtable-from-newsrc-alist)))
15882
15883 (defun gnus-make-newsrc-file (file)
15884   "Make server dependent file name by catenating FILE and server host name."
15885   (let* ((file (expand-file-name file nil))
15886          (real-file (concat file "-" (nth 1 gnus-select-method))))
15887     (if (or (file-exists-p real-file)
15888             (file-exists-p (concat real-file ".el"))
15889             (file-exists-p (concat real-file ".eld")))
15890         real-file file)))
15891
15892 (defun gnus-newsrc-to-gnus-format ()
15893   (setq gnus-newsrc-options "")
15894   (setq gnus-newsrc-options-n nil)
15895
15896   (or gnus-active-hashtb
15897       (setq gnus-active-hashtb (make-vector 4095 0)))
15898   (let ((buf (current-buffer))
15899         (already-read (> (length gnus-newsrc-alist) 1))
15900         group subscribed options-symbol newsrc Options-symbol
15901         symbol reads num1)
15902     (goto-char (point-min))
15903     ;; We intern the symbol `options' in the active hashtb so that we
15904     ;; can `eq' against it later.
15905     (set (setq options-symbol (intern "options" gnus-active-hashtb)) nil)
15906     (set (setq Options-symbol (intern "Options" gnus-active-hashtb)) nil)
15907
15908     (while (not (eobp))
15909       ;; We first read the first word on the line by narrowing and
15910       ;; then reading into `gnus-active-hashtb'.  Most groups will
15911       ;; already exist in that hashtb, so this will save some string
15912       ;; space.
15913       (narrow-to-region
15914        (point)
15915        (progn (skip-chars-forward "^ \t!:\n") (point)))
15916       (goto-char (point-min))
15917       (setq symbol
15918             (and (/= (point-min) (point-max))
15919                  (let ((obarray gnus-active-hashtb)) (read buf))))
15920       (widen)
15921       ;; Now, the symbol we have read is either `options' or a group
15922       ;; name.  If it is an options line, we just add it to a string.
15923       (cond
15924        ((or (eq symbol options-symbol)
15925             (eq symbol Options-symbol))
15926         (setq gnus-newsrc-options
15927               ;; This concating is quite inefficient, but since our
15928               ;; thorough studies show that approx 99.37% of all
15929               ;; .newsrc files only contain a single options line, we
15930               ;; don't give a damn, frankly, my dear.
15931               (concat gnus-newsrc-options
15932                       (buffer-substring
15933                        (gnus-point-at-bol)
15934                        ;; Options may continue on the next line.
15935                        (or (and (re-search-forward "^[^ \t]" nil 'move)
15936                                 (progn (beginning-of-line) (point)))
15937                            (point)))))
15938         (forward-line -1))
15939        (symbol
15940         ;; Group names can be just numbers.  
15941         (when (numberp symbol) 
15942           (setq symbol (intern (int-to-string symbol) gnus-active-hashtb)))
15943         (or (boundp symbol) (set symbol nil))
15944         ;; It was a group name.
15945         (setq subscribed (= (following-char) ?:)
15946               group (symbol-name symbol)
15947               reads nil)
15948         (if (eolp)
15949             ;; If the line ends here, this is clearly a buggy line, so
15950             ;; we put point a the beginning of line and let the cond
15951             ;; below do the error handling.
15952             (beginning-of-line)
15953           ;; We skip to the beginning of the ranges.
15954           (skip-chars-forward "!: \t"))
15955         ;; We are now at the beginning of the list of read articles.
15956         ;; We read them range by range.
15957         (while
15958             (cond
15959              ((looking-at "[0-9]+")
15960               ;; We narrow and read a number instead of buffer-substring/
15961               ;; string-to-int because it's faster.  narrow/widen is
15962               ;; faster than save-restriction/narrow, and save-restriction
15963               ;; produces a garbage object.
15964               (setq num1 (progn
15965                            (narrow-to-region (match-beginning 0) (match-end 0))
15966                            (read buf)))
15967               (widen)
15968               ;; If the next character is a dash, then this is a range.
15969               (if (= (following-char) ?-)
15970                   (progn
15971                     ;; We read the upper bound of the range.
15972                     (forward-char 1)
15973                     (if (not (looking-at "[0-9]+"))
15974                         ;; This is a buggy line, by we pretend that
15975                         ;; it's kinda OK.  Perhaps the user should be
15976                         ;; dinged?
15977                         (setq reads (cons num1 reads))
15978                       (setq reads
15979                             (cons
15980                              (cons num1
15981                                    (progn
15982                                      (narrow-to-region (match-beginning 0)
15983                                                        (match-end 0))
15984                                      (read buf)))
15985                              reads))
15986                       (widen)))
15987                 ;; It was just a simple number, so we add it to the
15988                 ;; list of ranges.
15989                 (setq reads (cons num1 reads)))
15990               ;; If the next char in ?\n, then we have reached the end
15991               ;; of the line and return nil.
15992               (/= (following-char) ?\n))
15993              ((= (following-char) ?\n)
15994               ;; End of line, so we end.
15995               nil)
15996              (t
15997               ;; Not numbers and not eol, so this might be a buggy
15998               ;; line...
15999               (or (eobp)
16000                   ;; If it was eob instead of ?\n, we allow it.
16001                   (progn
16002                     ;; The line was buggy.
16003                     (setq group nil)
16004                     (gnus-message 3 "Mangled line: %s"
16005                                   (buffer-substring (gnus-point-at-bol)
16006                                                     (gnus-point-at-eol)))
16007                     (ding)
16008                     (sit-for 1)))
16009               nil))
16010           ;; Skip past ", ".  Spaces are illegal in these ranges, but
16011           ;; we allow them, because it's a common mistake to put a
16012           ;; space after the comma.
16013           (skip-chars-forward ", "))
16014
16015         ;; We have already read .newsrc.eld, so we gently update the
16016         ;; data in the hash table with the information we have just
16017         ;; read.
16018         (when group
16019           (let ((info (gnus-get-info group))
16020                 level)
16021             (if info
16022                 ;; There is an entry for this file in the alist.
16023                 (progn
16024                   (gnus-info-set-read info (nreverse reads))
16025                   ;; We update the level very gently.  In fact, we
16026                   ;; only change it if there's been a status change
16027                   ;; from subscribed to unsubscribed, or vice versa.
16028                   (setq level (gnus-info-level info))
16029                   (cond ((and (<= level gnus-level-subscribed)
16030                               (not subscribed))
16031                          (setq level (if reads
16032                                          gnus-level-default-unsubscribed
16033                                        (1+ gnus-level-default-unsubscribed))))
16034                         ((and (> level gnus-level-subscribed) subscribed)
16035                          (setq level gnus-level-default-subscribed)))
16036                   (gnus-info-set-level info level))
16037               ;; This is a new group.
16038               (setq info (list group
16039                                (if subscribed
16040                                    gnus-level-default-subscribed
16041                                  (if reads
16042                                      (1+ gnus-level-subscribed)
16043                                    gnus-level-default-unsubscribed))
16044                                (nreverse reads))))
16045             (setq newsrc (cons info newsrc))))))
16046       (forward-line 1))
16047
16048     (setq newsrc (nreverse newsrc))
16049
16050     (if (not already-read)
16051         ()
16052       ;; We now have two newsrc lists - `newsrc', which is what we
16053       ;; have read from .newsrc, and `gnus-newsrc-alist', which is
16054       ;; what we've read from .newsrc.eld.  We have to merge these
16055       ;; lists.  We do this by "attaching" any (foreign) groups in the
16056       ;; gnus-newsrc-alist to the (native) group that precedes them.
16057       (let ((rc (cdr gnus-newsrc-alist))
16058             (prev gnus-newsrc-alist)
16059             entry mentry)
16060         (while rc
16061           (or (null (nth 4 (car rc)))   ; It's a native group.
16062               (assoc (caar rc) newsrc) ; It's already in the alist.
16063               (if (setq entry (assoc (caar prev) newsrc))
16064                   (setcdr (setq mentry (memq entry newsrc))
16065                           (cons (car rc) (cdr mentry)))
16066                 (setq newsrc (cons (car rc) newsrc))))
16067           (setq prev rc
16068                 rc (cdr rc)))))
16069
16070     (setq gnus-newsrc-alist newsrc)
16071     ;; We make the newsrc hashtb.
16072     (gnus-make-hashtable-from-newsrc-alist)
16073
16074     ;; Finally, if we read some options lines, we parse them.
16075     (or (string= gnus-newsrc-options "")
16076         (gnus-newsrc-parse-options gnus-newsrc-options))))
16077
16078 ;; Parse options lines to find "options -n !all rec.all" and stuff.
16079 ;; The return value will be a list on the form
16080 ;; ((regexp1 . ignore)
16081 ;;  (regexp2 . subscribe)...)
16082 ;; When handling new newsgroups, groups that match a `ignore' regexp
16083 ;; will be ignored, and groups that match a `subscribe' regexp will be
16084 ;; subscribed.  A line like
16085 ;; options -n !all rec.all
16086 ;; will lead to a list that looks like
16087 ;; (("^rec\\..+" . subscribe)
16088 ;;  ("^.+" . ignore))
16089 ;; So all "rec.*" groups will be subscribed, while all the other
16090 ;; groups will be ignored.  Note that "options -n !all rec.all" is very
16091 ;; different from "options -n rec.all !all".
16092 (defun gnus-newsrc-parse-options (options)
16093   (let (out eol)
16094     (save-excursion
16095       (gnus-set-work-buffer)
16096       (insert (regexp-quote options))
16097       ;; First we treat all continuation lines.
16098       (goto-char (point-min))
16099       (while (re-search-forward "\n[ \t]+" nil t)
16100         (replace-match " " t t))
16101       ;; Then we transform all "all"s into ".+"s.
16102       (goto-char (point-min))
16103       (while (re-search-forward "\\ball\\b" nil t)
16104         (replace-match ".+" t t))
16105       (goto-char (point-min))
16106       ;; We remove all other options than the "-n" ones.
16107       (while (re-search-forward "[ \t]-[^n][^-]*" nil t)
16108         (replace-match " ")
16109         (forward-char -1))
16110       (goto-char (point-min))
16111
16112       ;; We are only interested in "options -n" lines - we
16113       ;; ignore the other option lines.
16114       (while (re-search-forward "[ \t]-n" nil t)
16115         (setq eol
16116               (or (save-excursion
16117                     (and (re-search-forward "[ \t]-n" (gnus-point-at-eol) t)
16118                          (- (point) 2)))
16119                   (gnus-point-at-eol)))
16120         ;; Search for all "words"...
16121         (while (re-search-forward "[^ \t,\n]+" eol t)
16122           (if (= (char-after (match-beginning 0)) ?!)
16123               ;; If the word begins with a bang (!), this is a "not"
16124               ;; spec.  We put this spec (minus the bang) and the
16125               ;; symbol `ignore' into the list.
16126               (setq out (cons (cons (concat
16127                                      "^" (buffer-substring
16128                                           (1+ (match-beginning 0))
16129                                           (match-end 0)))
16130                                     'ignore) out))
16131             ;; There was no bang, so this is a "yes" spec.
16132             (setq out (cons (cons (concat "^" (match-string 0))
16133                                   'subscribe) out)))))
16134
16135       (setq gnus-newsrc-options-n out))))
16136
16137 (defun gnus-save-newsrc-file (&optional force)
16138   "Save .newsrc file."
16139   ;; Note: We cannot save .newsrc file if all newsgroups are removed
16140   ;; from the variable gnus-newsrc-alist.
16141   (when (and (or gnus-newsrc-alist gnus-killed-list)
16142              gnus-current-startup-file)
16143     (save-excursion
16144       (if (and (or gnus-use-dribble-file gnus-slave)
16145                (not force)
16146                (or (not gnus-dribble-buffer)
16147                    (not (buffer-name gnus-dribble-buffer))
16148                    (zerop (save-excursion
16149                             (set-buffer gnus-dribble-buffer)
16150                             (buffer-size)))))
16151           (gnus-message 4 "(No changes need to be saved)")
16152         (run-hooks 'gnus-save-newsrc-hook)
16153         (if gnus-slave
16154             (gnus-slave-save-newsrc)
16155           ;; Save .newsrc.
16156           (when gnus-save-newsrc-file
16157             (gnus-message 5 "Saving %s..." gnus-current-startup-file)
16158             (gnus-gnus-to-newsrc-format)
16159             (gnus-message 5 "Saving %s...done" gnus-current-startup-file))
16160           ;; Save .newsrc.eld.
16161           (set-buffer (get-buffer-create " *Gnus-newsrc*"))
16162           (make-local-variable 'version-control)
16163           (setq version-control 'never)
16164           (setq buffer-file-name
16165                 (concat gnus-current-startup-file ".eld"))
16166           (gnus-add-current-to-buffer-list)
16167           (buffer-disable-undo (current-buffer))
16168           (erase-buffer)
16169           (gnus-message 5 "Saving %s.eld..." gnus-current-startup-file)
16170           (gnus-gnus-to-quick-newsrc-format)
16171           (run-hooks 'gnus-save-quick-newsrc-hook)
16172           (save-buffer)
16173           (kill-buffer (current-buffer))
16174           (gnus-message
16175            5 "Saving %s.eld...done" gnus-current-startup-file))
16176         (gnus-dribble-delete-file)))))
16177
16178 (defun gnus-gnus-to-quick-newsrc-format ()
16179   "Insert Gnus variables such as gnus-newsrc-alist in lisp format."
16180   (insert ";; Gnus startup file.\n")
16181   (insert ";; Never delete this file - touch .newsrc instead to force Gnus\n")
16182   (insert ";; to read .newsrc.\n")
16183   (insert "(setq gnus-newsrc-file-version "
16184           (prin1-to-string gnus-version) ")\n")
16185   (let ((variables
16186          (if gnus-save-killed-list gnus-variable-list
16187            ;; Remove the `gnus-killed-list' from the list of variables
16188            ;; to be saved, if required.
16189            (delq 'gnus-killed-list (copy-sequence gnus-variable-list))))
16190         ;; Peel off the "dummy" group.
16191         (gnus-newsrc-alist (cdr gnus-newsrc-alist))
16192         variable)
16193     ;; Insert the variables into the file.
16194     (while variables
16195       (when (and (boundp (setq variable (pop variables)))
16196                  (symbol-value variable))
16197         (insert "(setq " (symbol-name variable) " '"
16198                 (prin1-to-string (symbol-value variable)) ")\n")))))
16199
16200 (defun gnus-gnus-to-newsrc-format ()
16201   ;; Generate and save the .newsrc file.
16202   (let ((newsrc (cdr gnus-newsrc-alist))
16203         info ranges range)
16204     (save-excursion
16205       (set-buffer (create-file-buffer gnus-current-startup-file))
16206       (setq buffer-file-name gnus-current-startup-file)
16207       (buffer-disable-undo (current-buffer))
16208       (erase-buffer)
16209       ;; Write options.
16210       (if gnus-newsrc-options (insert gnus-newsrc-options))
16211       ;; Write subscribed and unsubscribed.
16212       (while newsrc
16213         (setq info (car newsrc))
16214         (if (not (gnus-info-method info))
16215             ;; Don't write foreign groups to .newsrc.
16216             (progn
16217               (insert (gnus-info-group info)
16218                       (if (> (gnus-info-level info) gnus-level-subscribed)
16219                           "!" ":"))
16220               (if (setq ranges (gnus-info-read info))
16221                   (progn
16222                     (insert " ")
16223                     (if (not (listp (cdr ranges)))
16224                         (if (= (car ranges) (cdr ranges))
16225                             (insert (int-to-string (car ranges)))
16226                           (insert (int-to-string (car ranges)) "-"
16227                                   (int-to-string (cdr ranges))))
16228                       (while ranges
16229                         (setq range (car ranges)
16230                               ranges (cdr ranges))
16231                         (if (or (atom range) (= (car range) (cdr range)))
16232                             (insert (int-to-string
16233                                      (or (and (atom range) range)
16234                                          (car range))))
16235                           (insert (int-to-string (car range)) "-"
16236                                   (int-to-string (cdr range))))
16237                         (if ranges (insert ","))))))
16238               (insert "\n")))
16239         (setq newsrc (cdr newsrc)))
16240       (make-local-variable 'version-control)
16241       (setq version-control 'never)
16242       ;; It has been reported that sometime the modtime on the .newsrc
16243       ;; file seems to be off.  We really do want to overwrite it, so
16244       ;; we clear the modtime here before saving.  It's a bit odd,
16245       ;; though...
16246       ;; sometimes the modtime clear isn't sufficient.  most brute force:
16247       ;; delete the silly thing entirely first.  but this fails to provide
16248       ;; such niceties as .newsrc~ creation.
16249       (if gnus-modtime-botch
16250           (delete-file gnus-startup-file)
16251         (clear-visited-file-modtime))
16252       (run-hooks 'gnus-save-standard-newsrc-hook)
16253       (save-buffer)
16254       (kill-buffer (current-buffer)))))
16255
16256 \f
16257 ;;;
16258 ;;; Slave functions.
16259 ;;;
16260
16261 (defun gnus-slave-save-newsrc ()
16262   (save-excursion
16263     (set-buffer gnus-dribble-buffer)
16264     (let ((slave-name
16265            (make-temp-name (concat gnus-current-startup-file "-slave-"))))
16266       (write-region (point-min) (point-max) slave-name nil 'nomesg))))
16267
16268 (defun gnus-master-read-slave-newsrc ()
16269   (let ((slave-files
16270          (directory-files
16271           (file-name-directory gnus-current-startup-file)
16272           t (concat
16273              "^" (regexp-quote
16274                   (concat
16275                    (file-name-nondirectory gnus-current-startup-file)
16276                    "-slave-")))
16277           t))
16278         file)
16279     (if (not slave-files)
16280         ()                              ; There are no slave files to read.
16281       (gnus-message 7 "Reading slave newsrcs...")
16282       (save-excursion
16283         (set-buffer (get-buffer-create " *gnus slave*"))
16284         (buffer-disable-undo (current-buffer))
16285         (setq slave-files
16286               (sort (mapcar (lambda (file)
16287                               (list (nth 5 (file-attributes file)) file))
16288                             slave-files)
16289                     (lambda (f1 f2)
16290                       (or (< (caar f1) (caar f2))
16291                           (< (nth 1 (car f1)) (nth 1 (car f2)))))))
16292         (while slave-files
16293           (erase-buffer)
16294           (setq file (nth 1 (car slave-files)))
16295           (insert-file-contents file)
16296           (if (condition-case ()
16297                   (progn
16298                     (eval-buffer (current-buffer))
16299                     t)
16300                 (error
16301                  (gnus-message 3 "Possible error in %s" file)
16302                  (ding)
16303                  (sit-for 2)
16304                  nil))
16305               (or gnus-slave ; Slaves shouldn't delete these files.
16306                   (condition-case ()
16307                       (delete-file file)
16308                     (error nil))))
16309           (setq slave-files (cdr slave-files))))
16310       (gnus-message 7 "Reading slave newsrcs...done"))))
16311
16312 \f
16313 ;;;
16314 ;;; Group description.
16315 ;;;
16316
16317 (defun gnus-read-all-descriptions-files ()
16318   (let ((methods (cons gnus-select-method 
16319                        (nconc
16320                         (when gnus-message-archive-method
16321                           (list "archive"))
16322                         gnus-secondary-select-methods))))
16323     (while methods
16324       (gnus-read-descriptions-file (car methods))
16325       (setq methods (cdr methods)))
16326     t))
16327
16328 (defun gnus-read-descriptions-file (&optional method)
16329   (let ((method (or method gnus-select-method)))
16330     (when (stringp method)
16331       (setq method (gnus-server-to-method method)))
16332     ;; We create the hashtable whether we manage to read the desc file
16333     ;; to avoid trying to re-read after a failed read.
16334     (or gnus-description-hashtb
16335         (setq gnus-description-hashtb
16336               (gnus-make-hashtable (length gnus-active-hashtb))))
16337     ;; Mark this method's desc file as read.
16338     (gnus-sethash (gnus-group-prefixed-name "" method) "Has read"
16339                   gnus-description-hashtb)
16340
16341     (gnus-message 5 "Reading descriptions file via %s..." (car method))
16342     (cond
16343      ((not (gnus-check-server method))
16344       (gnus-message 1 "Couldn't open server")
16345       nil)
16346      ((not (gnus-request-list-newsgroups method))
16347       (gnus-message 1 "Couldn't read newsgroups descriptions")
16348       nil)
16349      (t
16350       (let (group)
16351         (save-excursion
16352           (save-restriction
16353             (set-buffer nntp-server-buffer)
16354             (goto-char (point-min))
16355             (if (or (search-forward "\n.\n" nil t)
16356                     (goto-char (point-max)))
16357                 (progn
16358                   (beginning-of-line)
16359                   (narrow-to-region (point-min) (point))))
16360             (goto-char (point-min))
16361             (while (not (eobp))
16362               ;; If we get an error, we set group to 0, which is not a
16363               ;; symbol...
16364               (setq group
16365                     (condition-case ()
16366                         (let ((obarray gnus-description-hashtb))
16367                           ;; Group is set to a symbol interned in this
16368                           ;; hash table.
16369                           (read nntp-server-buffer))
16370                       (error 0)))
16371               (skip-chars-forward " \t")
16372               ;; ...  which leads to this line being effectively ignored.
16373               (and (symbolp group)
16374                    (set group (buffer-substring
16375                                (point) (progn (end-of-line) (point)))))
16376               (forward-line 1))))
16377         (gnus-message 5 "Reading descriptions file...done")
16378         t)))))
16379
16380 (defun gnus-group-get-description (group)
16381   "Get the description of a group by sending XGTITLE to the server."
16382   (when (gnus-request-group-description group)
16383     (save-excursion
16384       (set-buffer nntp-server-buffer)
16385       (goto-char (point-min))
16386       (when (looking-at "[^ \t]+[ \t]+\\(.*\\)")
16387         (match-string 1)))))
16388
16389 \f
16390 ;;;
16391 ;;; Buffering of read articles.
16392 ;;;
16393
16394 (defvar gnus-backlog-buffer " *Gnus Backlog*")
16395 (defvar gnus-backlog-articles nil)
16396 (defvar gnus-backlog-hashtb nil)
16397
16398 (defun gnus-backlog-buffer ()
16399   "Return the backlog buffer."
16400   (or (get-buffer gnus-backlog-buffer)
16401       (save-excursion
16402         (set-buffer (get-buffer-create gnus-backlog-buffer))
16403         (buffer-disable-undo (current-buffer))
16404         (setq buffer-read-only t)
16405         (gnus-add-current-to-buffer-list)
16406         (get-buffer gnus-backlog-buffer))))
16407
16408 (defun gnus-backlog-setup ()
16409   "Initialize backlog variables."
16410   (unless gnus-backlog-hashtb
16411     (setq gnus-backlog-hashtb (make-vector 1023 0))))
16412
16413 (defun gnus-backlog-shutdown ()
16414   "Clear all backlog variables and buffers."
16415   (when (get-buffer gnus-backlog-buffer)
16416     (kill-buffer gnus-backlog-buffer))
16417   (setq gnus-backlog-hashtb nil
16418         gnus-backlog-articles nil))
16419
16420 (defun gnus-backlog-enter-article (group number buffer)
16421   (gnus-backlog-setup)
16422   (let ((ident (intern (concat group ":" (int-to-string number))
16423                        gnus-backlog-hashtb))
16424         b)
16425     (if (memq ident gnus-backlog-articles)
16426         () ; It's already kept.
16427       ;; Remove the oldest article, if necessary.
16428       (and (numberp gnus-keep-backlog)
16429            (>= (length gnus-backlog-articles) gnus-keep-backlog)
16430            (gnus-backlog-remove-oldest-article))
16431       (setq gnus-backlog-articles (cons ident gnus-backlog-articles))
16432       ;; Insert the new article.
16433       (save-excursion
16434         (set-buffer (gnus-backlog-buffer))
16435         (let (buffer-read-only)
16436           (goto-char (point-max))
16437           (or (bolp) (insert "\n"))
16438           (setq b (point))
16439           (insert-buffer-substring buffer)
16440           ;; Tag the beginning of the article with the ident.
16441           (put-text-property b (1+ b) 'gnus-backlog ident))))))
16442
16443 (defun gnus-backlog-remove-oldest-article ()
16444   (save-excursion
16445     (set-buffer (gnus-backlog-buffer))
16446     (goto-char (point-min))
16447     (if (zerop (buffer-size))
16448         () ; The buffer is empty.
16449       (let ((ident (get-text-property (point) 'gnus-backlog))
16450             buffer-read-only)
16451         ;; Remove the ident from the list of articles.
16452         (when ident
16453           (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16454         ;; Delete the article itself.
16455         (delete-region
16456          (point) (next-single-property-change
16457                   (1+ (point)) 'gnus-backlog nil (point-max)))))))
16458
16459 (defun gnus-backlog-request-article (group number buffer)
16460   (when (numberp number)
16461     (gnus-backlog-setup)
16462     (let ((ident (intern (concat group ":" (int-to-string number))
16463                          gnus-backlog-hashtb))
16464           beg end)
16465       (when (memq ident gnus-backlog-articles)
16466         ;; It was in the backlog.
16467         (save-excursion
16468           (set-buffer (gnus-backlog-buffer))
16469           (if (not (setq beg (text-property-any
16470                               (point-min) (point-max) 'gnus-backlog
16471                               ident)))
16472               ;; It wasn't in the backlog after all.
16473               (ignore
16474                (setq gnus-backlog-articles (delq ident gnus-backlog-articles)))
16475             ;; Find the end (i. e., the beginning of the next article).
16476             (setq end
16477                   (next-single-property-change
16478                    (1+ beg) 'gnus-backlog (current-buffer) (point-max)))))
16479         (let ((buffer-read-only nil))
16480           (erase-buffer)
16481           (insert-buffer-substring gnus-backlog-buffer beg end)
16482           t)))))
16483
16484 ;; Allow redefinition of Gnus functions.
16485
16486 (gnus-ems-redefine)
16487
16488 (provide 'gnus)
16489
16490 ;;; gnus.el ends here