*** empty log message ***
[gnus] / lisp / nnoo.el
1 ;;; nnoo.el --- OO Gnus Backends
2 ;; Copyright (C) 1996 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'nnheader)
29 (require 'cl)
30
31 (defvar nnoo-definition-alist nil)
32 (defvar nnoo-state-alist nil)
33
34 (defmacro defvoo (var init &optional doc &rest map)
35   "The same as `defvar', only takes list of variables to MAP to."
36   `(prog1
37        ,(if doc
38             `(defvar ,var ,init ,doc)
39           `(defvar ,var ,init))
40      (nnoo-define ',var ',map)))
41 (put 'defvoo 'lisp-indent-function 2)
42 (put 'defvoo 'edebug-form-spec '(var init &optional doc &rest map))
43
44 (defmacro deffoo (func args &rest forms)
45   "The same as `defun', only register FUNC."
46   `(prog1
47        (defun ,func ,args ,@forms)
48      (nnoo-register-function ',func)))
49 (put 'deffoo 'lisp-indent-function 2)
50 (put 'deffoo 'edebug-form-spec '(&define name lambda-list def-body))
51
52 (defun nnoo-register-function (func)
53   (let ((funcs (nthcdr 3 (assoc (nnoo-backend func)
54                                 nnoo-definition-alist))))
55     (unless funcs
56       (error "%s belongs to a backend that hasn't been declared" func))
57     (setcar funcs (cons func (car funcs)))))
58
59 (defmacro nnoo-declare (backend &rest parents)
60   `(eval-and-compile
61      (push (list ',backend 
62                  (mapcar (lambda (p) (list p)) ',parents)
63                  nil nil)
64            nnoo-definition-alist)))
65 (put 'nnoo-declare 'lisp-indent-function 1)
66
67 (defun nnoo-parents (backend)
68   (nth 1 (assoc backend nnoo-definition-alist)))
69
70 (defun nnoo-variables (backend)
71   (nth 2 (assoc backend nnoo-definition-alist)))
72
73 (defun nnoo-functions (backend)
74   (nth 3 (assoc backend nnoo-definition-alist)))
75
76 (defmacro nnoo-import (backend &rest imports)
77   `(nnoo-import-1 ',backend ',imports))
78 (put 'nnoo-import 'lisp-indent-function 1)
79
80 (defun nnoo-import-1 (backend imports)
81   (let ((call-function
82          (if (symbolp (car imports)) (pop imports) 'nnoo-parent-function))
83         imp functions function)
84     (while (setq imp (pop imports))
85       (setq functions
86             (or (cdr imp)
87                 (nnoo-functions (car imp))))
88       (while functions
89         (unless (fboundp (setq function
90                                (nnoo-symbol backend (nnoo-rest-symbol
91                                                      (car functions)))))
92           (eval `(deffoo ,function (&rest args)
93                    (,call-function ',backend ',(car functions) args))))
94         (pop functions)))))
95
96 (defun nnoo-parent-function (backend function args)
97   (let* ((pbackend (nnoo-backend function)))
98     (nnoo-change-server pbackend (nnoo-current-server backend)
99                         (cdr (assq pbackend (nnoo-parents backend))))
100     (apply function args)))
101
102 (defun nnoo-execute (backend function &rest args)
103   "Execute FUNCTION on behalf of BACKEND."
104   (let* ((pbackend (nnoo-backend function)))
105     (nnoo-change-server pbackend (nnoo-current-server backend)
106                         (cdr (assq pbackend (nnoo-parents backend))))
107     (apply function args)))
108
109 (defmacro nnoo-map-functions (backend &rest maps)
110   `(nnoo-map-functions-1 ',backend ',maps))
111 (put 'nnoo-map-functions 'lisp-indent-function 1)
112
113 (defun nnoo-map-functions-1 (backend maps)
114   (let (m margs i)
115     (while (setq m (pop maps))
116       (setq i 0
117             margs nil)
118       (while (< i (length (cdr m)))
119         (if (numberp (nth i (cdr m)))
120             (push `(nth ,i args) margs)
121           (push (nth i (cdr m)) margs))
122         (incf i))
123       (eval `(deffoo ,(nnoo-symbol backend (nnoo-rest-symbol (car m)))
124                  (&rest args)
125                (nnoo-parent-function ',backend ',(car m)
126                                      ,(cons 'list (nreverse margs))))))))
127   
128 (defun nnoo-backend (symbol)
129   (string-match "^[^-]+-" (symbol-name symbol))
130   (intern (substring (symbol-name symbol) 0 (1- (match-end 0)))))
131
132 (defun nnoo-rest-symbol (symbol)
133   (string-match "^[^-]+-" (symbol-name symbol))
134   (intern (substring (symbol-name symbol) (match-end 0))))
135
136 (defun nnoo-symbol (backend symbol)
137   (intern (format "%s-%s" backend symbol)))
138
139 (defun nnoo-define (var map)
140   (let* ((backend (nnoo-backend var))
141          (def (assq backend nnoo-definition-alist))
142          (parents (nth 1 def)))
143     (unless def
144       (error "%s belongs to a backend that hasn't been declared." var))
145     (setcar (nthcdr 2 def)
146             (delq (assq var (nth 2 def)) (nth 2 def)))
147     (setcar (nthcdr 2 def)
148             (cons (cons var (symbol-value var))
149                   (nth 2 def)))
150     (while map
151       (nconc (assq (nnoo-backend (car map)) parents)
152              (list (list (pop map) var))))))
153
154 (defun nnoo-change-server (backend server defs)
155   (let* ((bstate (cdr (assq backend nnoo-state-alist)))
156          (current (car bstate))
157          (parents (nnoo-parents backend))
158          (bvariables (nnoo-variables backend))
159          state def)
160     (unless bstate
161       (push (setq bstate (list backend nil))
162             nnoo-state-alist)
163       (pop bstate))
164     (if (equal server current)
165         t
166       (nnoo-push-server backend current)
167       (setq state (or (cdr (assoc server (cddr bstate)))
168                       (nnoo-variables backend)))
169       (while state
170         (set (caar state) (cdar state))
171         (pop state))
172       (setcar bstate server)
173       (unless (cdr (assoc server (cddr bstate)))
174         (while (setq def (pop defs))
175           (unless (assq (car def) bvariables)
176             (nconc bvariables
177                    (list (cons (car def) (and (boundp (car def))
178                                               (symbol-value (car def)))))))
179           (set (car def) (cadr def))))
180       (while parents
181         (nnoo-change-server 
182          (caar parents) server 
183          (mapcar (lambda (def) (list (car def) (symbol-value (cadr def))))
184                  (cdar parents)))
185         (pop parents))))
186   t)
187
188 (defun nnoo-push-server (backend current)
189   (let ((bstate (assq backend nnoo-state-alist))
190         (defs (nnoo-variables backend)))
191     ;; Remove the old definition.
192     (setcdr (cdr bstate) (delq (assoc current (cddr bstate)) (cddr bstate)))
193     ;; If this is the first time we push the server (i. e., this is 
194     ;; the nil server), then we update the default values of
195     ;; all the variables to reflect the current values.
196     (unless current
197       (let ((defaults (nnoo-variables backend))
198             def)
199         (while (setq def (pop defaults))
200           (setcdr def (symbol-value (car def))))))
201     (let (state)
202       (while defs
203         (push (cons (caar defs) (symbol-value (caar defs)))
204               state)
205         (pop defs))
206       (nconc bstate (list (cons current state))))))
207
208 (defun nnoo-current-server-p (backend server)
209   (equal (nnoo-current-server backend) server))
210
211 (defun nnoo-current-server (backend)
212   (nth 1 (assq backend nnoo-state-alist)))
213
214 (defun nnoo-close-server (backend &optional server)
215   (unless server
216     (setq server (nnoo-current-server backend)))
217   (when server
218     (let* ((bstate (cdr (assq backend nnoo-state-alist)))
219            (defs (assoc server (cdr bstate))))
220       (when bstate
221         (setcar bstate nil)
222         (setcdr bstate (delq defs (cdr bstate)))
223         (pop defs)
224         (while defs
225           (set (car (pop defs)) nil)))))
226   t)
227
228 (defun nnoo-close (backend)
229   (setq nnoo-state-alist
230         (delq (assq backend nnoo-state-alist)
231               nnoo-state-alist))
232   t)
233
234 (defun nnoo-status-message (backend server)
235   (nnheader-get-report backend))
236
237 (defun nnoo-server-opened (backend server)
238   (and (nnoo-current-server-p backend server)
239        nntp-server-buffer
240        (buffer-name nntp-server-buffer)))
241
242 (defmacro nnoo-define-basics (backend)
243   "Define `close-server', `server-opened' and `status-message'."
244   `(eval-and-compile
245      (nnoo-define-basics-1 ',backend)))
246
247 (defun nnoo-define-basics-1 (backend)
248   (let ((functions '(close-server server-opened status-message)))
249     (while functions
250       (eval `(deffoo ,(nnoo-symbol backend (car functions))
251                  (&optional server)
252                (,(nnoo-symbol 'nnoo (pop functions)) ',backend server)))))
253   (eval `(deffoo ,(nnoo-symbol backend 'open-server)
254              (server &optional defs)
255            (nnoo-change-server ',backend server defs))))
256
257 (defmacro nnoo-define-skeleton (backend)
258   "Define all required backend functions for BACKEND.
259 All functions will return nil and report an error."
260   `(eval-and-compile
261      (nnoo-define-skeleton-1 ',backend)))
262
263 (defun nnoo-define-skeleton-1 (backend)
264   (let ((functions '(retrieve-headers
265                      request-close request-article
266                      open-group request-group close-group
267                      request-list request-post request-list-newsgroups))
268         function fun)
269     (while (setq function (pop functions))
270       (when (not (fboundp (setq fun (nnoo-symbol backend function))))
271         (eval `(deffoo ,fun
272                    (&rest args)
273                  (nnheader-report ',backend ,(format "%s-%s not implemented"
274                                                      backend function))))))))
275 (provide 'nnoo)
276
277 ;;; nnoo.el ends here.