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