Initial revision
[emacs.git] / lisp / nnoo.el
blobcddba4ae564cfc1155d96a45656e4e97b2b83f6b
1 ;;; nnoo.el --- OO Gnus Backends
2 ;; Copyright (C) 1996 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Keywords: news
7 ;; This file is part of GNU Emacs.
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.
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.
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.
24 ;;; Commentary:
26 ;;; Code:
28 (eval-when-compile (require 'cl))
30 (defvar nnoo-definition-alist nil)
31 (defvar nnoo-state-alist nil)
33 (defmacro defvoo (var init &optional doc &rest map)
34 "The same as `defvar', only takes list of variables to MAP to."
35 `(prog1
36 ,(if doc
37 `(defvar ,var ,init ,doc)
38 `(defvar ,var ,init))
39 (nnoo-define ',var ',map)))
40 (put 'defvoo 'lisp-indent-function 2)
41 (put 'defvoo 'lisp-indent-hook 2)
42 (put 'defvoo 'edebug-form-spec '(var init &optional doc &rest map))
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 'lisp-indent-hook 2)
51 (put 'deffoo 'edebug-form-spec '(&define name lambda-list def-body))
53 (defun nnoo-register-function (func)
54 (let ((funcs (nthcdr 3 (assoc (nnoo-backend func)
55 nnoo-definition-alist))))
56 (unless funcs
57 (error "%s belongs to a backend that hasn't been declared" func))
58 (setcar funcs (cons func (car funcs)))))
60 (defmacro nnoo-declare (backend &rest parents)
61 `(eval-and-compile
62 (push (list ',backend
63 (mapcar (lambda (p) (list p)) ',parents)
64 nil nil)
65 nnoo-definition-alist)))
66 (put 'nnoo-declare 'lisp-indent-function 1)
67 (put 'nnoo-declare 'lisp-indent-hook 1)
69 (defun nnoo-parents (backend)
70 (nth 1 (assoc backend nnoo-definition-alist)))
72 (defun nnoo-variables (backend)
73 (nth 2 (assoc backend nnoo-definition-alist)))
75 (defun nnoo-functions (backend)
76 (nth 3 (assoc backend nnoo-definition-alist)))
78 (defmacro nnoo-import (backend &rest imports)
79 `(nnoo-import-1 ',backend ',imports))
80 (put 'nnoo-import 'lisp-indent-function 1)
81 (put 'nnoo-import 'lisp-indent-hook 1)
83 (defun nnoo-import-1 (backend imports)
84 (let ((call-function
85 (if (symbolp (car imports)) (pop imports) 'nnoo-parent-function))
86 imp functions function)
87 (while (setq imp (pop imports))
88 (setq functions
89 (or (cdr imp)
90 (nnoo-functions (car imp))))
91 (while functions
92 (unless (fboundp (setq function
93 (nnoo-symbol backend (nnoo-rest-symbol
94 (car functions)))))
95 (eval `(deffoo ,function (&rest args)
96 (,call-function ',backend ',(car functions) args))))
97 (pop functions)))))
99 (defun nnoo-parent-function (backend function args)
100 (let* ((pbackend (nnoo-backend function)))
101 (nnoo-change-server pbackend (nnoo-current-server backend)
102 (cdr (assq pbackend (nnoo-parents backend))))
103 (apply function args)))
105 (defun nnoo-execute (backend function &rest args)
106 "Execute FUNCTION on behalf of BACKEND."
107 (let* ((pbackend (nnoo-backend function)))
108 (nnoo-change-server pbackend (nnoo-current-server backend)
109 (cdr (assq pbackend (nnoo-parents backend))))
110 (apply function args)))
112 (defmacro nnoo-map-functions (backend &rest maps)
113 `(nnoo-map-functions-1 ',backend ',maps))
114 (put 'nnoo-map-functions 'lisp-indent-function 1)
115 (put 'nnoo-map-functions 'lisp-indent-hook 1)
117 (defun nnoo-map-functions-1 (backend maps)
118 (let (m margs i)
119 (while (setq m (pop maps))
120 (setq i 0
121 margs nil)
122 (while (< i (length (cdr m)))
123 (if (numberp (nth i (cdr m)))
124 (push `(nth ,i args) margs)
125 (push (nth i (cdr m)) margs))
126 (incf i))
127 (eval `(deffoo ,(nnoo-symbol backend (nnoo-rest-symbol (car m)))
128 (&rest args)
129 (nnoo-parent-function ',backend ',(car m)
130 ,(cons 'list (nreverse margs))))))))
132 (defun nnoo-backend (symbol)
133 (string-match "^[^-]+-" (symbol-name symbol))
134 (intern (substring (symbol-name symbol) 0 (1- (match-end 0)))))
136 (defun nnoo-rest-symbol (symbol)
137 (string-match "^[^-]+-" (symbol-name symbol))
138 (intern (substring (symbol-name symbol) (match-end 0))))
140 (defun nnoo-symbol (backend symbol)
141 (intern (format "%s-%s" backend symbol)))
143 (defun nnoo-define (var map)
144 (let* ((backend (nnoo-backend var))
145 (def (assq backend nnoo-definition-alist))
146 (parents (nth 1 def)))
147 (unless def
148 (error "%s belongs to a backend that hasn't been declared." var))
149 (setcar (nthcdr 2 def)
150 (delq (assq var (nth 2 def)) (nth 2 def)))
151 (setcar (nthcdr 2 def)
152 (cons (cons var (symbol-value var))
153 (nth 2 def)))
154 (while map
155 (nconc (assq (nnoo-backend (car map)) parents)
156 (list (list (pop map) var))))))
158 (defun nnoo-change-server (backend server defs)
159 (let* ((bstate (cdr (assq backend nnoo-state-alist)))
160 (sdefs (assq backend nnoo-definition-alist))
161 (current (car bstate))
162 (parents (nnoo-parents backend))
163 state)
164 (unless bstate
165 (push (setq bstate (list backend nil))
166 nnoo-state-alist)
167 (pop bstate))
168 (if (equal server current)
170 (nnoo-push-server backend current)
171 (setq state (or (cdr (assoc server (cddr bstate)))
172 (nnoo-variables backend)))
173 (while state
174 (set (caar state) (cdar state))
175 (pop state))
176 (setcar bstate server)
177 (unless (cdr (assoc server (cddr bstate)))
178 (while defs
179 (set (caar defs) (cadar defs))
180 (pop defs)))
181 (while parents
182 (nnoo-change-server
183 (caar parents) server
184 (mapcar (lambda (def) (list (car def) (symbol-value (cadr def))))
185 (cdar parents)))
186 (pop parents))))
189 (defun nnoo-push-server (backend current)
190 (let ((bstate (assq backend nnoo-state-alist))
191 (defs (nnoo-variables backend)))
192 ;; Remove the old definition.
193 (setcdr (cdr bstate) (delq (assoc current (cddr bstate)) (cddr bstate)))
194 (let (state)
195 (while defs
196 (push (cons (caar defs) (symbol-value (caar defs)))
197 state)
198 (pop defs))
199 (nconc bstate (list (cons current state))))))
201 (defun nnoo-current-server-p (backend server)
202 (equal (nnoo-current-server backend) server))
204 (defun nnoo-current-server (backend)
205 (nth 1 (assq backend nnoo-state-alist)))
207 (defun nnoo-close-server (backend &optional server)
208 (unless server
209 (setq server (nnoo-current-server backend)))
210 (when server
211 (let* ((bstate (cdr (assq backend nnoo-state-alist)))
212 (defs (assoc server (cdr bstate))))
213 (when bstate
214 (setcar bstate nil)
215 (setcdr bstate (delq defs (cdr bstate)))
216 (pop defs)
217 (while defs
218 (set (car (pop defs)) nil)))))
221 (defun nnoo-close (backend)
222 (setq nnoo-state-alist
223 (delq (assq backend nnoo-state-alist)
224 nnoo-state-alist))
227 (defun nnoo-status-message (backend server)
228 (nnheader-get-report backend))
230 (defun nnoo-server-opened (backend server)
231 (and (nnoo-current-server-p backend server)
232 nntp-server-buffer
233 (buffer-name nntp-server-buffer)))
235 (defmacro nnoo-define-basics (backend)
236 `(eval-and-compile
237 (nnoo-define-basics-1 ',backend)))
239 (defun nnoo-define-basics-1 (backend)
240 (let ((functions '(close-server server-opened status-message)))
241 (while functions
242 (eval `(deffoo ,(nnoo-symbol backend (car functions))
243 (&optional server)
244 (,(nnoo-symbol 'nnoo (pop functions)) ',backend server)))))
245 (eval `(deffoo ,(nnoo-symbol backend 'open-server)
246 (server &optional defs)
247 (nnoo-change-server ',backend server defs))))
249 (provide 'nnoo)
251 ;;; nnoo.el ends here.