defvar.el 11.8 KB
Newer Older
David Byers's avatar
David Byers committed
1
;;;;; -*-coding: iso-8859-1;-*-
David Byers's avatar
X    
David Byers committed
2
3
;;;;;
;;;;; $Id$
4
;;;;; Copyright (C) 1991-2002  Lysator Academic Computer Association.
David Byers's avatar
X    
David Byers committed
5
;;;;;
6
;;;;; This file is part of the LysKOM Emacs LISP client.
David Byers's avatar
X    
David Byers committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
;;;;; 
;;;;; LysKOM is free software; you can redistribute it and/or modify it
;;;;; under the terms of the GNU General Public License as published by 
;;;;; the Free Software Foundation; either version 2, or (at your option) 
;;;;; any later version.
;;;;; 
;;;;; LysKOM is distributed in the hope that it will be useful, but WITHOUT
;;;;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
;;;;; FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
;;;;; for more details.
;;;;; 
;;;;; You should have received a copy of the GNU General Public License
;;;;; along with LysKOM; see the file COPYING.  If not, write to
;;;;; Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
;;;;; or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
;;;;; MA 02139, USA.
;;;;;
;;;;; Please mail bug reports to bug-lyskom@lysator.liu.se. 
;;;;;
;;;; ================================================================
;;;; ================================================================
;;;;
;;;; File: defvar.el
;;;; Authos: David Byers
;;;;
;;;; This file contains definitions used to define variables
;;;;


36
(defvar lyskom-clientversion-long 
David Byers's avatar
X    
David Byers committed
37
38
39
40
41
42
  "$Id$\n"
  "Version for every file in the client.")


(provide 'lyskom)

43
;; Just to get rid of a compiler warning
David Byers's avatar
David Byers committed
44
(defvar kom-dont-read-saved-variables)
David Byers's avatar
X    
David Byers committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

(defvar lyskom-local-variables nil
  "List of variables to make local in a LysKOM buffer")

(defvar lyskom-local-hooks nil
  "List of hooks to make local in a LysKOM buffer.")

(defvar lyskom-protected-variables nil
  "List of variables that are protected from kill-buffer")

(defvar lyskom-inherited-variables nil
  "List of variables inherited from the LysKOM buffer")

(defvar lyskom-elisp-variables nil
  "Tells the client what flags and hooks that are to be saved in the server.
These are the flags that are saved in the elisp-client part of the server.")

62
63
64
65
(defvar lyskom-transition-variables nil
  "Tells the client what variables are not to be saved in the server, but
are to be read from the server. This is for transitioning.")

David Byers's avatar
David Byers committed
66
67
68
(defvar lyskom-copy-transition-variables nil
  "Tells the client about variables that need transforming when copying.")

David Byers's avatar
X    
David Byers committed
69
70
71
72
73
74
75
(defvar lyskom-minibuffer-variables nil
  "These are variables that should be set in the minibuffer by 
lyskom-with-lyskom-minibuffer.")

(defvar lyskom-minibuffer-values nil
  "Dynamic binding of values that minibuffer variables are to take on")

76
77
78
79
80
(defvar lyskom-global-variables nil
  "List of flags that are to be saved as booleans in the common block.

Don't change these. They are defined by the protocol.")

David Byers's avatar
David Byers committed
81
82
83
(defvar lyskom-non-portable-server-variables nil
  "List of variables that should not be copied from server to server.")

David Byers's avatar
X    
David Byers committed
84
85
86
87
88
89
90
91
92

(defmacro lyskom-save-variables (var-list &rest forms)
  "Save the values and property list of symbols in VAR-LIST and execute FORMS
The symbol value, property list and buffer-local property of all variables
is saved before executing FORMS and restored when FORMS have finished."
  (let ((sym1 (make-symbol "lyskom-saved-variables"))
        (sym2 (make-symbol "lyskom-saved-symbols"))
        (sym3 (make-symbol "lyskom-saved-local"))
        (sym4 (make-symbol "lyskom-saved-plist")))
David Byers's avatar
David Byers committed
93
94
95
96
97
98
    `(let* ((,sym2 (quote ,var-list))
	    (,sym1 (mapcar 'symbol-value ,sym2))
	    (,sym4 (mapcar 'symbol-plist ,sym2))
	    (,sym3 (mapcar (lambda (v)
			     (local-variable-p v (current-buffer)))
			   ,sym2)))
David Byers's avatar
X    
David Byers committed
99
         (unwind-protect 
David Byers's avatar
David Byers committed
100
101
102
103
104
105
106
107
108
             (progn ,@forms)
           (while ,sym1
             (when (car ,sym3) (make-local-variable (car ,sym2)))
             (set (car ,sym2) (car ,sym1))
             (setplist (car ,sym2) (car ,sym4))
             (setq ,sym1 (cdr ,sym1)
                   ,sym2 (cdr ,sym2)
                   ,sym3 (cdr ,sym3)
                   ,sym4 (cdr ,sym4)))))))
David Byers's avatar
X    
David Byers committed
109
110
111
112
113
114

(put 'lyskom-save-variables 'edebug-form-spec
     '(sexp body))

(defmacro lyskom-with-lyskom-minibuffer (&rest forms)
  "Run FORMS after ensuring that LysKOM minibuffer variables will be set."
David Byers's avatar
David Byers committed
115
116
117
118
119
120
121
122
123
  `(let* ((lyskom-minibuffer-values
	   (mapcar 'symbol-value lyskom-minibuffer-variables)))
     (unwind-protect
	 (progn
	   (add-hook 'minibuffer-setup-hook
		     'lyskom-setup-minibuffer-variables)
	   ,@forms)
       (remove-hook 'minibuffer-setup-hook
		    'lyskom-setup-minibuffer-variables))))
David Byers's avatar
X    
David Byers committed
124
125
126
127
128
129
130
131
132
133
134
135

(put 'lyskom-with-lyskom-minibuffer 'edebug-form-spec
     '(body))


(defun lyskom-setup-minibuffer-variables ()
  (let ((syms lyskom-minibuffer-variables)
        (vals lyskom-minibuffer-values))
    (while syms
      (make-local-variable (car syms))
      (set (car syms) (car vals))
      (setq syms (cdr syms)
David Byers's avatar
David Byers committed
136
            vals (cdr vals)))))
David Byers's avatar
X    
David Byers committed
137
138
139
140
141
142
143
144
145
146

         

(defmacro def-kom-var (name value &rest args)
    "Define a variable with name NAME and initial value VALUE.
Remaining args, ARGS may be
A string        Used as the documentation string for the variable
A symbol        A predefined property of the variable
A list          A widget specification for the variable

David Byers's avatar
David Byers committed
147
Predefined (and tested) properties are the following
David Byers's avatar
X    
David Byers committed
148
149
150
151
152
153
server          Save the variable in the elisp block. Implies local.
local           Make the variable buffer-local.
inherited       The variable is inherited from parent buffer. Implies protected
protected       The variable is marked as permanent local. Implies local.
minibuffer      Inherit the variable as a local variable in the minibuffer.
server-hook     A hook stored in the server.
David Byers's avatar
David Byers committed
154
155
local-hook      A hook variable that is made local in LysKOM buffers.
language-force  A language-variable whose value is to be forced."
David Byers's avatar
X    
David Byers committed
156
157
158
159
          
    (let ((inherited nil)
          (protected nil)
          (elisp-block nil)
160
          (transition-block nil)
David Byers's avatar
David Byers committed
161
          (copy-transition-block nil)
David Byers's avatar
X    
David Byers committed
162
163
164
165
          (buffer-local nil)
          (widget-spec nil)
          (doc-string nil)
          (minibuffer nil)
David Byers's avatar
David Byers committed
166
          (non-portable nil)
David Byers's avatar
David Byers committed
167
168
169
          (local-hook-doc nil)
          (local-var-doc nil)
          (server-doc nil)
David Byers's avatar
David Byers committed
170
          (language-force nil)
David Byers's avatar
X    
David Byers committed
171
          (arglist args))
172
;      (message "%S" name)
David Byers's avatar
X    
David Byers committed
173
174
175
176
      (while arglist
        (cond ((stringp (car arglist)) (setq doc-string (car arglist)))
              ((consp (car arglist))
               (setq widget-spec 
David Byers's avatar
David Byers committed
177
178
179
                     `((setq lyskom-custom-variables
			     (cons  (quote ,(list name (car arglist)))
				    lyskom-custom-variables)))))
David Byers's avatar
X    
David Byers committed
180
181
              ((symbolp (car arglist))
               (cond ((eq (car arglist) 'server)
David Byers's avatar
David Byers committed
182
                      (setq local-var-doc t server-doc t)
David Byers's avatar
X    
David Byers committed
183
                      (setq elisp-block
David Byers's avatar
David Byers committed
184
185
                            `((add-to-list 'lyskom-elisp-variables ',name)
                              (add-to-list 'lyskom-local-variables ',name))))
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204

                     ((eq (car arglist) 'common)
                      (setq local-var-doc t server-doc t)
                      (let ((common-name (car (cdr arglist)))
                            (type (car (cdr (cdr arglist)))))
                        (setq arglist (cdr (cdr arglist)))
                        (setq elisp-block
                              `((add-to-list 'lyskom-local-variables ',name)
                                (add-to-list 'lyskom-global-variables
                                             (vector ',common-name ',name ',type))))))

                     ((eq (car arglist) 'transition)
                      (let ((converter (car (cdr arglist))))
                        (setq arglist (cdr arglist))
                        (setq local-var-doc t server-doc t)
                        (setq transition-block
                              `((add-to-list 'lyskom-transition-variables 
                                             '(,name . ,converter))
                                (add-to-list 'lyskom-local-variables ',name)))))
David Byers's avatar
X    
David Byers committed
205

David Byers's avatar
David Byers committed
206
207
208
209
210
211
212
213
                     ((eq (car arglist) 'copy-transition)
                      (let ((converter (car (cdr arglist))))
                        (setq arglist (cdr arglist))
                        (setq local-var-doc t server-doc t)
                        (setq copy-transition-block
                              `((add-to-list 'lyskom-copy-transition-variables 
                                             '(,name . ,converter))))))

David Byers's avatar
X    
David Byers committed
214
                     ((eq (car arglist) 'server-hook)
David Byers's avatar
David Byers committed
215
                      (setq local-hook-doc t server-doc t)
David Byers's avatar
X    
David Byers committed
216
                      (setq elisp-block
David Byers's avatar
David Byers committed
217
218
                            `((add-to-list 'lyskom-elisp-variables ',name)
			      (add-to-list 'lyskom-local-hooks ',name))))
David Byers's avatar
X    
David Byers committed
219
220

                     ((eq (car arglist) 'protected)
David Byers's avatar
David Byers committed
221
                      (setq local-var-doc t)
David Byers's avatar
X    
David Byers committed
222
                      (setq protected
David Byers's avatar
David Byers committed
223
224
225
                            `((put ',name 'permanent-local t)
			      (add-to-list 'lyskom-protected-variables ',name)
			      (add-to-list 'lyskom-local-variables ',name))))
David Byers's avatar
X    
David Byers committed
226
227

                     ((eq (car arglist) 'inherited)
David Byers's avatar
David Byers committed
228
                      (setq local-var-doc t)
David Byers's avatar
X    
David Byers committed
229
                      (setq inherited
David Byers's avatar
David Byers committed
230
231
232
233
                            `((add-to-list 'lyskom-inherited-variables ',name)
			      (put ',name 'permanent-local t)
			      (add-to-list 'lyskom-protected-variables ',name)
			      (add-to-list 'lyskom-local-variables ',name))))
David Byers's avatar
X    
David Byers committed
234
235

                     ((eq (car arglist) 'local)
David Byers's avatar
David Byers committed
236
                      (setq local-var-doc t)
David Byers's avatar
X    
David Byers committed
237
                      (setq buffer-local
David Byers's avatar
David Byers committed
238
                            `((add-to-list 'lyskom-local-variables ',name))))
David Byers's avatar
X    
David Byers committed
239
240

                     ((eq (car arglist) 'local-hook)
David Byers's avatar
David Byers committed
241
                      (setq local-hook-doc t)
David Byers's avatar
X    
David Byers committed
242
                      (setq buffer-local
David Byers's avatar
David Byers committed
243
244
245
246
247
                            `((add-to-list 'lyskom-local-hooks ',name))))

                     ((eq (car arglist) 'non-portable)
                      (setq non-portable
                            `((add-to-list 'lyskom-non-portable-server-variables ',name))))
David Byers's avatar
X    
David Byers committed
248
249
250

                     ((eq (car arglist) 'minibuffer)
                      (setq minibuffer
David Byers's avatar
David Byers committed
251
                            `((add-to-list 'lyskom-minibuffer-variables ',name))))
David Byers's avatar
X    
David Byers committed
252

David Byers's avatar
David Byers committed
253
254
255
256
                     ((eq (car arglist) 'language-force)
                      (setq language-force
                            `((put ',name 'lyskom-language-force t))))

David Byers's avatar
X    
David Byers committed
257
258
259
260
261
                     (t (error "LysKOM: Unknown variable property: %S"
                               (car arglist)))))
              (t (error "LysKOM: Strange variable argument type: %S" 
                        (car arglist))))
        (setq arglist (cdr arglist)))
David Byers's avatar
David Byers committed
262

David Byers's avatar
David Byers committed
263
264
      (if (null doc-string) (setq doc-string "This variable is not documented."))

265
266
267
268
269
270
271
272
273
      (if doc-string
        (if
          (if (or local-var-doc local-hook-doc)
            (setq doc-string (concat doc-string "\n")))
          (if local-var-doc
            (setq doc-string (concat doc-string "\nThis variable is buffer-local.")))
          (if local-hook-doc
            (setq doc-string (concat doc-string "\nThis variable is a buffer-local hook.")))
          (setq doc-string (concat doc-string "\n\n\
David Byers's avatar
David Byers committed
274
Setting this variable in .emacs may not yield the results you expect
275
since that will affect all LysKOM sessions."))))
David Byers's avatar
David Byers committed
276

277
      (if (and doc-string server-doc)
David Byers's avatar
David Byers committed
278
279
280
281
282
283
        (setq doc-string (concat doc-string "

This variable is normally stored on a per-session basis in the
LysKOM server, but can be set in your .emacs simply by setting
it using setq or defvar.")))

David Byers's avatar
David Byers committed
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
      `(progn (dont-compile (if (and (boundp ',name)
				     (or (not (boundp lyskom-is-loaded))
					 (not lyskom-is-loaded))
				     (listp kom-dont-read-saved-variables))
				(add-to-list 'kom-dont-read-saved-variables ',name)))
	      (defvar ,name ,value ,doc-string)
	      ,@(apply 'append
		       (list inherited
			     protected
			     elisp-block
			     transition-block
			     copy-transition-block
			     buffer-local
			     minibuffer
			     non-portable
			     widget-spec
			     language-force
			     )))))
302

David Byers's avatar
X    
David Byers committed
303
304
305
306
307
308

(put 'def-kom-var 'edebug-form-spec
     '(&define name form &rest sexp))



309
(eval-and-compile (provide 'lyskom-defvar))