*** 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 (defconst gnus-version-number "0.14"
32   "Version number for this version of Gnus.")
33
34 (defconst gnus-version (format "Red Gnus v%s" gnus-version-number)
35   "Version string for this version of Gnus.")
36
37 (defvar gnus-inhibit-startup-message nil
38   "*If non-nil, the startup message will not be displayed.")
39
40 ;;; Internal variables
41
42 (defvar gnus-group-buffer "*Group*")
43
44 ;;; Splash screen.
45
46 (defun gnus-splash ()
47   (save-excursion
48     (switch-to-buffer gnus-group-buffer)
49     (let ((buffer-read-only nil))
50       (erase-buffer)
51       (unless gnus-inhibit-startup-message
52         (gnus-group-startup-message)
53         (sit-for 0)))))
54
55 (defun gnus-indent-rigidly (start end arg)
56   "Indent rigidly using only spaces and no tabs."
57   (save-excursion
58     (save-restriction
59       (narrow-to-region start end)
60       (indent-rigidly start end arg)
61       ;; We translate tabs into spaces -- not everybody uses
62       ;; an 8-character tab.
63       (goto-char (point-min))
64       (while (search-forward "\t" nil t)
65         (replace-match "        " t t)))))
66
67 (defun gnus-group-startup-message (&optional x y)
68   "Insert startup message in current buffer."
69   ;; Insert the message.
70   (erase-buffer)
71   (insert
72    (format "              %s
73           _    ___ _             _
74           _ ___ __ ___  __    _ ___
75           __   _     ___    __  ___
76               _           ___     _
77              _  _ __             _
78              ___   __            _
79                    __           _
80                     _      _   _
81                    _      _    _
82                       _  _    _
83                   __  ___
84                  _   _ _     _
85                 _   _
86               _    _
87              _    _
88             _
89           __
90
91 "
92            ""))
93   ;; And then hack it.
94   (gnus-indent-rigidly (point-min) (point-max)
95                        (/ (max (- (window-width) (or x 46)) 0) 2))
96   (goto-char (point-min))
97   (forward-line 1)
98   (let* ((pheight (count-lines (point-min) (point-max)))
99          (wheight (window-height))
100          (rest (- wheight pheight)))
101     (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n)))
102   ;; Fontify some.
103   (goto-char (point-min))
104   (and (search-forward "Praxis" nil t)
105        (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
106   (goto-char (point-min))
107   (setq mode-line-buffer-identification gnus-version)
108   (set-buffer-modified-p t))
109
110 (eval-when (load)
111   (gnus-splash))
112
113 ;;; Do the rest.
114
115 (require 'gnus-load)
116
117 \f
118
119 ;; Fix by Hallvard B Furuseth <h.b.furuseth@usit.uio.no>.
120 ;; If you want the cursor to go somewhere else, set these two
121 ;; functions in some startup hook to whatever you want.
122 (defalias 'gnus-summary-position-point 'gnus-goto-colon)
123 (defalias 'gnus-group-position-point 'gnus-goto-colon)
124
125 ;;; Various macros and substs.
126
127 (defun gnus-header-from (header)
128   (mail-header-from header))
129
130 (defmacro gnus-gethash (string hashtable)
131   "Get hash value of STRING in HASHTABLE."
132   `(symbol-value (intern-soft ,string ,hashtable)))
133
134 (defmacro gnus-sethash (string value hashtable)
135   "Set hash value.  Arguments are STRING, VALUE, and HASHTABLE."
136   `(set (intern ,string ,hashtable) ,value))
137
138 (defmacro gnus-group-unread (group)
139   "Get the currently computed number of unread articles in GROUP."
140   `(car (gnus-gethash ,group gnus-newsrc-hashtb)))
141
142 (defmacro gnus-group-entry (group)
143   "Get the newsrc entry for GROUP."
144   `(gnus-gethash ,group gnus-newsrc-hashtb))
145
146 (defmacro gnus-active (group)
147   "Get active info on GROUP."
148   `(gnus-gethash ,group gnus-active-hashtb))
149
150 (defmacro gnus-set-active (group active)
151   "Set GROUP's active info."
152   `(gnus-sethash ,group ,active gnus-active-hashtb))
153
154 (defun gnus-alive-p ()
155   "Say whether Gnus is running or not."
156   (and gnus-group-buffer
157        (get-buffer gnus-group-buffer)
158        (save-excursion
159          (set-buffer gnus-group-buffer)
160          (eq major-mode 'gnus-group-mode))))
161
162 ;; Info access macros.
163
164 (defmacro gnus-info-group (info)
165   `(nth 0 ,info))
166 (defmacro gnus-info-rank (info)
167   `(nth 1 ,info))
168 (defmacro gnus-info-read (info)
169   `(nth 2 ,info))
170 (defmacro gnus-info-marks (info)
171   `(nth 3 ,info))
172 (defmacro gnus-info-method (info)
173   `(nth 4 ,info))
174 (defmacro gnus-info-params (info)
175   `(nth 5 ,info))
176
177 (defmacro gnus-info-level (info)
178   `(let ((rank (gnus-info-rank ,info)))
179      (if (consp rank)
180          (car rank)
181        rank)))
182 (defmacro gnus-info-score (info)
183   `(let ((rank (gnus-info-rank ,info)))
184      (or (and (consp rank) (cdr rank)) 0)))
185
186 (defmacro gnus-info-set-group (info group)
187   `(setcar ,info ,group))
188 (defmacro gnus-info-set-rank (info rank)
189   `(setcar (nthcdr 1 ,info) ,rank))
190 (defmacro gnus-info-set-read (info read)
191   `(setcar (nthcdr 2 ,info) ,read))
192 (defmacro gnus-info-set-marks (info marks &optional extend)
193   (if extend
194       `(gnus-info-set-entry ,info ,marks 3)
195     `(setcar (nthcdr 3 ,info) ,marks)))
196 (defmacro gnus-info-set-method (info method &optional extend)
197   (if extend
198       `(gnus-info-set-entry ,info ,method 4)
199     `(setcar (nthcdr 4 ,info) ,method)))
200 (defmacro gnus-info-set-params (info params &optional extend)
201   (if extend
202       `(gnus-info-set-entry ,info ,params 5)
203     `(setcar (nthcdr 5 ,info) ,params)))
204
205 (defun gnus-info-set-entry (info entry number)
206   ;; Extend the info until we have enough elements.
207   (while (< (length info) number)
208     (nconc info (list nil)))
209   ;; Set the entry.
210   (setcar (nthcdr number info) entry))
211
212 (defmacro gnus-info-set-level (info level)
213   `(let ((rank (cdr ,info)))
214      (if (consp (car rank))
215          (setcar (car rank) ,level)
216        (setcar rank ,level))))
217 (defmacro gnus-info-set-score (info score)
218   `(let ((rank (cdr ,info)))
219      (if (consp (car rank))
220          (setcdr (car rank) ,score)
221        (setcar rank (cons (car rank) ,score)))))
222
223 (defmacro gnus-get-info (group)
224   `(nth 2 (gnus-gethash ,group gnus-newsrc-hashtb)))
225
226 ;; Byte-compiler warning.
227 (defvar gnus-visual)
228 ;; Find out whether the gnus-visual TYPE is wanted.
229 (defun gnus-visual-p (&optional type class)
230   (and gnus-visual                      ; Has to be non-nil, at least.
231        (if (not type)                   ; We don't care about type.
232            gnus-visual
233          (if (listp gnus-visual)        ; It's a list, so we check it.
234              (or (memq type gnus-visual)
235                  (memq class gnus-visual))
236            t))))
237
238 ;;; Load the compatability functions.
239
240 (require 'gnus-cus)
241 (require 'gnus-ems)
242
243 \f
244 ;;;
245 ;;; Shutdown
246 ;;;
247
248 (defvar gnus-shutdown-alist nil)
249
250 (defun gnus-add-shutdown (function &rest symbols)
251   "Run FUNCTION whenever one of SYMBOLS is shut down."
252   (push (cons function symbols) gnus-shutdown-alist))
253
254 (defun gnus-shutdown (symbol)
255   "Shut down everything that waits for SYMBOL."
256   (let ((alist gnus-shutdown-alist)
257         entry)
258     (while (setq entry (pop alist))
259       (when (memq symbol (cdr entry))
260         (funcall (car entry))))))
261
262 \f
263 ;;;
264 ;;; Gnus Utility Functions
265 ;;;
266
267 ;; Add the current buffer to the list of buffers to be killed on exit.
268 (defun gnus-add-current-to-buffer-list ()
269   (or (memq (current-buffer) gnus-buffer-list)
270       (setq gnus-buffer-list (cons (current-buffer) gnus-buffer-list))))
271
272 (defun gnus-version (&optional arg)
273   "Version number of this version of Gnus.
274 If ARG, insert string at point."
275   (interactive "P")
276   (let ((methods gnus-valid-select-methods)
277         (mess gnus-version)
278         meth)
279     ;; Go through all the legal select methods and add their version
280     ;; numbers to the total version string.  Only the backends that are
281     ;; currently in use will have their message numbers taken into
282     ;; consideration.
283     (while methods
284       (setq meth (intern (concat (caar methods) "-version")))
285       (and (boundp meth)
286            (stringp (symbol-value meth))
287            (setq mess (concat mess "; " (symbol-value meth))))
288       (setq methods (cdr methods)))
289     (if arg
290         (insert (message mess))
291       (message mess))))
292
293 (defun gnus-continuum-version (version)
294   "Return VERSION as a floating point number."
295   (when (or (string-match "^\\([^ ]+\\)? ?Gnus v?\\([0-9.]+\\)$" version)
296             (string-match "^\\(.?\\)gnus-\\([0-9.]+\\)$" version))
297     (let* ((alpha (and (match-beginning 1) (match-string 1 version)))
298            (number (match-string 2 version))
299            major minor least)
300       (string-match "\\([0-9]\\)\\.\\([0-9]+\\)\\.?\\([0-9]+\\)?" number)
301       (setq major (string-to-number (match-string 1 number)))
302       (setq minor (string-to-number (match-string 2 number)))
303       (setq least (if (match-beginning 3)
304                       (string-to-number (match-string 3 number))
305                     0))
306       (string-to-number
307        (if (zerop major)
308            (format "%s00%02d%02d"
309                    (cond 
310                     ((member alpha '("(ding)" "d")) "4.99")
311                     ((member alpha '("September" "s")) "5.01")
312                     ((member alpha '("Red" "r")) "5.03"))
313                    minor least)
314          (format "%d.%02d%02d" major minor least))))))
315
316 (defun gnus-info-find-node ()
317   "Find Info documentation of Gnus."
318   (interactive)
319   ;; Enlarge info window if needed.
320   (let (gnus-info-buffer)
321     (Info-goto-node (cadr (assq major-mode gnus-info-nodes)))
322     (setq gnus-info-buffer (current-buffer))
323     (gnus-configure-windows 'info)))
324
325 ;;; More various functions.
326
327 (defun gnus-group-read-only-p (&optional group)
328   "Check whether GROUP supports editing or not.
329 If GROUP is nil, `gnus-newsgroup-name' will be checked instead.  Note
330 that that variable is buffer-local to the summary buffers."
331   (let ((group (or group gnus-newsgroup-name)))
332     (not (gnus-check-backend-function 'request-replace-article group))))
333
334 (defun gnus-group-total-expirable-p (group)
335   "Check whether GROUP is total-expirable or not."
336   (let ((params (gnus-group-find-parameter group)))
337     (or (memq 'total-expire params)
338         (cdr (assq 'total-expire params)) ; (total-expire . t)
339         (and gnus-total-expirable-newsgroups ; Check var.
340              (string-match gnus-total-expirable-newsgroups group)))))
341
342 (defun gnus-group-auto-expirable-p (group)
343   "Check whether GROUP is total-expirable or not."
344   (let ((params (gnus-group-find-parameter group)))
345     (or (memq 'auto-expire params)
346         (cdr (assq 'auto-expire params)) ; (auto-expire . t)
347         (and gnus-auto-expirable-newsgroups ; Check var.
348              (string-match gnus-auto-expirable-newsgroups group)))))
349
350 (defun gnus-virtual-group-p (group)
351   "Say whether GROUP is virtual or not."
352   (memq 'virtual (assoc (symbol-name (car (gnus-find-method-for-group group)))
353                         gnus-valid-select-methods)))
354
355 (defun gnus-news-group-p (group &optional article)
356   "Return non-nil if GROUP (and ARTICLE) come from a news server."
357   (or (gnus-member-of-valid 'post group) ; Ordinary news group.
358       (and (gnus-member-of-valid 'post-mail group) ; Combined group.
359            (eq (gnus-request-type group article) 'news))))
360
361 ;; Returns a list of writable groups.
362 (defun gnus-writable-groups ()
363   (let ((alist gnus-newsrc-alist)
364         groups group)
365     (while (setq group (car (pop alist)))
366       (unless (gnus-group-read-only-p group)
367         (push group groups)))
368     (nreverse groups)))
369
370 ;; Check whether to use long file names.
371 (defun gnus-use-long-file-name (symbol)
372   ;; The variable has to be set...
373   (and gnus-use-long-file-name
374        ;; If it isn't a list, then we return t.
375        (or (not (listp gnus-use-long-file-name))
376            ;; If it is a list, and the list contains `symbol', we
377            ;; return nil.
378            (not (memq symbol gnus-use-long-file-name)))))
379
380 ;; Generate a unique new group name.
381 (defun gnus-generate-new-group-name (leaf)
382   (let ((name leaf)
383         (num 0))
384     (while (gnus-gethash name gnus-newsrc-hashtb)
385       (setq name (concat leaf "<" (int-to-string (setq num (1+ num))) ">")))
386     name))
387
388 (defun gnus-ephemeral-group-p (group)
389   "Say whether GROUP is ephemeral or not."
390   (gnus-group-get-parameter group 'quit-config))
391
392 (defun gnus-group-quit-config (group)
393   "Return the quit-config of GROUP."
394   (gnus-group-get-parameter group 'quit-config))
395
396 (defun gnus-simplify-mode-line ()
397   "Make mode lines a bit simpler."
398   (setq mode-line-modified "-- ")
399   (when (listp mode-line-format)
400     (make-local-variable 'mode-line-format)
401     (setq mode-line-format (copy-sequence mode-line-format))
402     (when (equal (nth 3 mode-line-format) "   ")
403       (setcar (nthcdr 3 mode-line-format) " "))))
404
405 ;;; Servers and groups.
406
407 (defsubst gnus-server-add-address (method)
408   (let ((method-name (symbol-name (car method))))
409     (if (and (memq 'address (assoc method-name gnus-valid-select-methods))
410              (not (assq (intern (concat method-name "-address")) method)))
411         (append method (list (list (intern (concat method-name "-address"))
412                                    (nth 1 method))))
413       method)))
414
415 (defsubst gnus-server-get-method (group method)
416   ;; Input either a server name, and extended server name, or a
417   ;; select method, and return a select method.
418   (cond ((stringp method)
419          (gnus-server-to-method method))
420         ((equal method gnus-select-method)
421          gnus-select-method)
422         ((and (stringp (car method)) group)
423          (gnus-server-extend-method group method))
424         ((and method (not group)
425               (equal (cadr method) ""))
426          method)
427         (t
428          (gnus-server-add-address method))))
429
430 (defun gnus-server-to-method (server)
431   "Map virtual server names to select methods."
432   (or 
433    ;; Is this a method, perhaps?
434    (and server (listp server) server)
435    ;; Perhaps this is the native server?
436    (and (equal server "native") gnus-select-method)
437    ;; It should be in the server alist.
438    (cdr (assoc server gnus-server-alist))
439    ;; If not, we look through all the opened server
440    ;; to see whether we can find it there.
441    (let ((opened gnus-opened-servers))
442      (while (and opened
443                  (not (equal server (format "%s:%s" (caaar opened)
444                                             (cadaar opened)))))
445        (pop opened))
446      (caar opened))))
447
448 (defmacro gnus-method-equal (ss1 ss2)
449   "Say whether two servers are equal."
450   `(let ((s1 ,ss1)
451          (s2 ,ss2))
452      (or (equal s1 s2)
453          (and (= (length s1) (length s2))
454               (progn
455                 (while (and s1 (member (car s1) s2))
456                   (setq s1 (cdr s1)))
457                 (null s1))))))
458
459 (defun gnus-server-equal (m1 m2)
460   "Say whether two methods are equal."
461   (let ((m1 (cond ((null m1) gnus-select-method)
462                   ((stringp m1) (gnus-server-to-method m1))
463                   (t m1)))
464         (m2 (cond ((null m2) gnus-select-method)
465                   ((stringp m2) (gnus-server-to-method m2))
466                   (t m2))))
467     (gnus-method-equal m1 m2)))
468
469 (defun gnus-servers-using-backend (backend)
470   "Return a list of known servers using BACKEND."
471   (let ((opened gnus-opened-servers)
472         out)
473     (while opened
474       (when (eq backend (caaar opened))
475         (push (caar opened) out))
476       (pop opened))
477     out))
478
479 (defun gnus-archive-server-wanted-p ()
480   "Say whether the user wants to use the archive server."
481   (cond 
482    ((or (not gnus-message-archive-method)
483         (not gnus-message-archive-group))
484     nil)
485    ((and gnus-message-archive-method gnus-message-archive-group)
486     t)
487    (t
488     (let ((active (cadr (assq 'nnfolder-active-file
489                               gnus-message-archive-method))))
490       (and active
491            (file-exists-p active))))))
492
493 (defun gnus-group-prefixed-name (group method)
494   "Return the whole name from GROUP and METHOD."
495   (and (stringp method) (setq method (gnus-server-to-method method)))
496   (if (not method)
497       group
498     (concat (format "%s" (car method))
499             (if (and
500                  (or (assoc (format "%s" (car method)) 
501                             (gnus-methods-using 'address))
502                      (gnus-server-equal method gnus-message-archive-method))
503                  (nth 1 method)
504                  (not (string= (nth 1 method) "")))
505                 (concat "+" (nth 1 method)))
506             ":" group)))
507
508 (defun gnus-group-real-prefix (group)
509   "Return the prefix of the current group name."
510   (if (string-match "^[^:]+:" group)
511       (substring group 0 (match-end 0))
512     ""))
513
514 (defun gnus-group-method (group)
515   "Return the server or method used for selecting GROUP."
516   (let ((prefix (gnus-group-real-prefix group)))
517     (if (equal prefix "")
518         gnus-select-method
519       (let ((servers gnus-opened-servers)
520             (server "")
521             backend possible found)
522         (if (string-match "^[^\\+]+\\+" prefix)
523             (setq backend (intern (substring prefix 0 (1- (match-end 0))))
524                   server (substring prefix (match-end 0) (1- (length prefix))))
525           (setq backend (intern (substring prefix 0 (1- (length prefix))))))
526         (while servers
527           (when (eq (caaar servers) backend)
528             (setq possible (caar servers))
529             (when (equal (cadaar servers) server)
530               (setq found (caar servers))))
531           (pop servers))
532         (or (car (rassoc found gnus-server-alist))
533             found
534             (car (rassoc possible gnus-server-alist))
535             possible
536             (list backend server))))))
537
538 (defsubst gnus-secondary-method-p (method)
539   "Return whether METHOD is a secondary select method."
540   (let ((methods gnus-secondary-select-methods)
541         (gmethod (gnus-server-get-method nil method)))
542     (while (and methods
543                 (not (equal (gnus-server-get-method nil (car methods))
544                             gmethod)))
545       (setq methods (cdr methods)))
546     methods))
547
548 (defun gnus-group-foreign-p (group)
549   "Say whether a group is foreign or not."
550   (and (not (gnus-group-native-p group))
551        (not (gnus-group-secondary-p group))))
552
553 (defun gnus-group-native-p (group)
554   "Say whether the group is native or not."
555   (not (string-match ":" group)))
556
557 (defun gnus-group-secondary-p (group)
558   "Say whether the group is secondary or not."
559   (gnus-secondary-method-p (gnus-find-method-for-group group)))
560
561 (defun gnus-group-find-parameter (group &optional symbol)
562   "Return the group parameters for GROUP.
563 If SYMBOL, return the value of that symbol in the group parameters."
564   (save-excursion
565     (set-buffer gnus-group-buffer)
566     (let ((parameters (funcall gnus-group-get-parameter-function group)))
567       (if symbol
568           (gnus-group-parameter-value parameters symbol)
569         parameters))))
570
571 (defun gnus-group-get-parameter (group &optional symbol)
572   "Return the group parameters for GROUP.
573 If SYMBOL, return the value of that symbol in the group parameters."
574   (let ((params (gnus-info-params (gnus-get-info group))))
575     (if symbol
576         (gnus-group-parameter-value params symbol)
577       params)))
578
579 (defun gnus-group-parameter-value (params symbol)
580   "Return the value of SYMBOL in group PARAMS."
581   (or (car (memq symbol params))        ; It's either a simple symbol
582       (cdr (assq symbol params))))      ; or a cons.
583
584 (defun gnus-group-add-parameter (group param)
585   "Add parameter PARAM to GROUP."
586   (let ((info (gnus-get-info group)))
587     (if (not info)
588         () ; This is a dead group.  We just ignore it.
589       ;; Cons the new param to the old one and update.
590       (gnus-group-set-info (cons param (gnus-info-params info))
591                            group 'params))))
592
593 (defun gnus-group-set-parameter (group name value)
594   "Set parameter NAME to VALUE in GROUP."
595   (let ((info (gnus-get-info group)))
596     (if (not info)
597         () ; This is a dead group.  We just ignore it.
598       (let ((old-params (gnus-info-params info))
599             (new-params (list (cons name value))))
600         (while old-params
601           (if (or (not (listp (car old-params)))
602                   (not (eq (caar old-params) name)))
603               (setq new-params (append new-params (list (car old-params)))))
604           (setq old-params (cdr old-params)))
605         (gnus-group-set-info new-params group 'params)))))
606
607 (defun gnus-group-add-score (group &optional score)
608   "Add SCORE to the GROUP score.
609 If SCORE is nil, add 1 to the score of GROUP."
610   (let ((info (gnus-get-info group)))
611     (when info
612       (gnus-info-set-score info (+ (gnus-info-score info) (or score 1))))))
613
614 ;; Function written by Stainless Steel Rat <ratinox@peorth.gweep.net>
615 (defun gnus-short-group-name (group &optional levels)
616   "Collapse GROUP name LEVELS.
617 Select methods are stripped and any remote host name is stripped down to
618 just the host name."
619   (let* ((name "") (foreign "") (depth -1) (skip 1)
620          (levels (or levels
621                      (progn
622                        (while (string-match "\\." group skip)
623                          (setq skip (match-end 0)
624                                depth (+ depth 1)))
625                        depth))))
626     ;; separate foreign select method from group name and collapse.
627     ;; if method contains a server, collapse to non-domain server name,
628     ;; otherwise collapse to select method
629     (if (string-match ":" group)
630         (cond ((string-match "+" group)
631                (let* ((plus (string-match "+" group))
632                       (colon (string-match ":" group))
633                       (dot (string-match "\\." group)))
634                  (setq foreign (concat
635                                 (substring group (+ 1 plus)
636                                            (cond ((null dot) colon)
637                                                  ((< colon dot) colon)
638                                                  ((< dot colon) dot))) ":")
639                        group (substring group (+ 1 colon))
640                        )))
641               (t
642                (let* ((colon (string-match ":" group)))
643                  (setq foreign (concat (substring group 0 (+ 1 colon)))
644                        group (substring group (+ 1 colon)))
645                  ))))
646     ;; collapse group name leaving LEVELS uncollapsed elements
647     (while group
648       (if (and (string-match "\\." group) (> levels 0))
649           (setq name (concat name (substring group 0 1))
650                 group (substring group (match-end 0))
651                 levels (- levels 1)
652                 name (concat name "."))
653         (setq name (concat foreign name group)
654               group nil)))
655     name))
656
657
658 \f
659 ;;;
660 ;;; Kill file handling.
661 ;;;
662
663 (defun gnus-apply-kill-file ()
664   "Apply a kill file to the current newsgroup.
665 Returns the number of articles marked as read."
666   (if (or (file-exists-p (gnus-newsgroup-kill-file nil))
667           (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
668       (gnus-apply-kill-file-internal)
669     0))
670
671 (defun gnus-kill-save-kill-buffer ()
672   (let ((file (gnus-newsgroup-kill-file gnus-newsgroup-name)))
673     (when (get-file-buffer file)
674       (save-excursion
675         (set-buffer (get-file-buffer file))
676         (and (buffer-modified-p) (save-buffer))
677         (kill-buffer (current-buffer))))))
678
679 (defvar gnus-kill-file-name "KILL"
680   "Suffix of the kill files.")
681
682 (defun gnus-newsgroup-kill-file (newsgroup)
683   "Return the name of a kill file name for NEWSGROUP.
684 If NEWSGROUP is nil, return the global kill file name instead."
685   (cond 
686    ;; The global KILL file is placed at top of the directory.
687    ((or (null newsgroup)
688         (string-equal newsgroup ""))
689     (expand-file-name gnus-kill-file-name
690                       gnus-kill-files-directory))
691    ;; Append ".KILL" to newsgroup name.
692    ((gnus-use-long-file-name 'not-kill)
693     (expand-file-name (concat (gnus-newsgroup-savable-name newsgroup)
694                               "." gnus-kill-file-name)
695                       gnus-kill-files-directory))
696    ;; Place "KILL" under the hierarchical directory.
697    (t
698     (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
699                               "/" gnus-kill-file-name)
700                       gnus-kill-files-directory))))
701
702 ;;; Server things.
703
704 (defun gnus-member-of-valid (symbol group)
705   "Find out if GROUP has SYMBOL as part of its \"valid\" spec."
706   (memq symbol (assoc
707                 (symbol-name (car (gnus-find-method-for-group group)))
708                 gnus-valid-select-methods)))
709
710 (defun gnus-method-option-p (method option)
711   "Return non-nil if select METHOD has OPTION as a parameter."
712   (when (stringp method)
713     (setq method (gnus-server-to-method method)))
714   (memq option (assoc (format "%s" (car method))
715                       gnus-valid-select-methods)))
716
717 (defun gnus-server-extend-method (group method)
718   ;; This function "extends" a virtual server.  If the server is
719   ;; "hello", and the select method is ("hello" (my-var "something"))
720   ;; in the group "alt.alt", this will result in a new virtual server
721   ;; called "hello+alt.alt".
722   (let ((entry
723          (gnus-copy-sequence
724           (if (gnus-server-equal method gnus-select-method) gnus-select-method
725             (cdr (assoc (car method) gnus-server-alist))))))
726     (if (not entry)
727         method
728       (setcar (cdr entry) (concat (nth 1 entry) "+" group))
729       (nconc entry (cdr method)))))
730
731 (defun gnus-server-status (method)
732   "Return the status of METHOD."
733   (nth 1 (assoc method gnus-opened-servers)))
734
735 (defun gnus-group-name-to-method (group)
736   "Return a select method suitable for GROUP."
737   (if (string-match ":" group)
738       (let ((server (substring group 0 (match-beginning 0))))
739         (if (string-match "\\+" server)
740             (list (intern (substring server 0 (match-beginning 0)))
741                   (substring server (match-end 0)))
742           (list (intern server) "")))
743     gnus-select-method))
744
745 (defun gnus-find-method-for-group (group &optional info)
746   "Find the select method that GROUP uses."
747   (or gnus-override-method
748       (and (not group)
749            gnus-select-method)
750       (let ((info (or info (gnus-get-info group)))
751             method)
752         (if (or (not info)
753                 (not (setq method (gnus-info-method info)))
754                 (equal method "native"))
755             gnus-select-method
756           (setq method
757                 (cond ((stringp method)
758                        (gnus-server-to-method method))
759                       ((stringp (car method))
760                        (gnus-server-extend-method group method))
761                       (t
762                        method)))
763           (cond ((equal (cadr method) "")
764                  method)
765                 ((null (cadr method))
766                  (list (car method) ""))
767                 (t
768                  (gnus-server-add-address method)))))))
769
770 (defun gnus-check-backend-function (func group)
771   "Check whether GROUP supports function FUNC."
772   (let ((method (if (stringp group) (car (gnus-find-method-for-group group))
773                   group)))
774     (fboundp (intern (format "%s-%s" method func)))))
775
776 (defun gnus-methods-using (feature)
777   "Find all methods that have FEATURE."
778   (let ((valids gnus-valid-select-methods)
779         outs)
780     (while valids
781       (if (memq feature (car valids))
782           (setq outs (cons (car valids) outs)))
783       (setq valids (cdr valids)))
784     outs))
785
786 (defun gnus-read-method (prompt)
787   "Prompt the user for a method.
788 Allow completion over sensible values."
789   (let ((method
790          (completing-read
791           prompt (append gnus-valid-select-methods gnus-server-alist)
792           nil t nil 'gnus-method-history)))
793     (cond 
794      ((equal method "")
795       (setq method gnus-select-method))
796      ((assoc method gnus-valid-select-methods)
797       (list (intern method)
798             (if (memq 'prompt-address
799                       (assoc method gnus-valid-select-methods))
800                 (read-string "Address: ")
801               "")))
802      ((assoc method gnus-server-alist)
803       method)
804      (t
805       (list (intern method) "")))))
806
807 ;;; User-level commands.
808
809 ;;;###autoload
810 (defun gnus-slave-no-server (&optional arg)
811   "Read network news as a slave, without connecting to local server"
812   (interactive "P")
813   (gnus-no-server arg t))
814
815 ;;;###autoload
816 (defun gnus-no-server (&optional arg slave)
817   "Read network news.
818 If ARG is a positive number, Gnus will use that as the
819 startup level.  If ARG is nil, Gnus will be started at level 2.
820 If ARG is non-nil and not a positive number, Gnus will
821 prompt the user for the name of an NNTP server to use.
822 As opposed to `gnus', this command will not connect to the local server."
823   (interactive "P")
824   (gnus-no-server-1 arg slave))
825
826 ;;;###autoload
827 (defun gnus-slave (&optional arg)
828   "Read news as a slave."
829   (interactive "P")
830   (gnus arg nil 'slave))
831
832 ;;;###autoload
833 (defun gnus-other-frame (&optional arg)
834   "Pop up a frame to read news."
835   (interactive "P")
836   (if (get-buffer gnus-group-buffer)
837       (let ((pop-up-frames t))
838         (gnus arg))
839     (select-frame (make-frame))
840     (gnus arg)))
841
842 ;;;###autoload
843 (defun gnus (&optional arg dont-connect slave)
844   "Read network news.
845 If ARG is non-nil and a positive number, Gnus will use that as the
846 startup level.  If ARG is non-nil and not a positive number, Gnus will
847 prompt the user for the name of an NNTP server to use."
848   (interactive "P")
849   (gnus-1 arg dont-connect slave))
850
851 ;; Allow redefinition of Gnus functions.
852
853 (gnus-ems-redefine)
854
855 (provide 'gnus)
856
857 ;;; gnus.el ends here