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