macros.el 11.3 KB
Newer Older
Linus Tolke's avatar
Linus Tolke committed
1
2
;;;;;
;;;;; $Id$
3
;;;;; Copyright (C) 1991, 1996  Lysator Academic Computer Association.
Linus Tolke's avatar
Linus Tolke committed
4
5
6
7
8
;;;;;
;;;;; This file is part of the LysKOM server.
;;;;; 
;;;;; LysKOM is free software; you can redistribute it and/or modify it
;;;;; under the terms of the GNU General Public License as published by 
9
;;;;; the Free Software Foundation; either version 2, or (at your option) 
Linus Tolke's avatar
Linus Tolke committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
;;;;; 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. 
;;;;;
Per Cederqvist's avatar
.    
Per Cederqvist committed
25
26
27
28
29
30
31
32
33
;;;; ================================================================
;;;; ================================================================
;;;;
;;;; File: macros.el
;;;;
;;;; This file contains the macros which must be loaded before lyskom can
;;;; be compiled.
;;;;

34
35
36
(setq lyskom-clientversion-long
      (concat lyskom-clientversion-long
	      "$Id$\n"))
37
38


39
40
41
42
43
44
45
46
47
;;; ======================================================================
;;; Use lyskom-provide to supply a definition that is only to be used
;;; if no definition already exists. The definition will be evaluated at
;;; both compile and run time.
;;;
;;; lyskom-provide-macros behaves like defmacro
;;; lyskom-provide-function behaves like defun
;;; lyskom-provide-subst behaves like defsubst
;;;
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

(defmacro lyskom-provide (definer name rest)
  (` (progn (eval-when-compile 
              (if (not (fboundp (quote (, name))))
                  (message "Compatibility %S for %S"
                           (quote (, definer))
                           (quote (, name)))))
            (eval-and-compile 
              (if (not (fboundp (quote (, name))))
                  ((, definer) (, name) (,@ rest)))))))

(defmacro lyskom-provide-macro (name &rest rest)
  (` (lyskom-provide defmacro (, name) (, rest))))

(defmacro lyskom-provide-function (name &rest rest)
  (` (lyskom-provide defun (, name) (, rest))))


(defmacro lyskom-provide-subst (name &rest rest)
  (` (lyskom-provide defsubst (, name) (, rest))))


70
;;; ======================================================================
Per Cederqvist's avatar
.    
Per Cederqvist committed
71
;;; lyskom-traverse - traverse a sequence.
72
;;;
Per Cederqvist's avatar
.    
Per Cederqvist committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

(defmacro lyskom-traverse (atom sequence &rest body)
  "Bind ATOM to each element in SEQUENCE and execute BODY.
Value returned is always nil."
  (list 'let* (list '(__i__ 0)
		    (list '__sequence__ sequence)
		    '(__len__ (length __sequence__))
		    atom)
	(list 'if '(listp __sequence__)
	      (append (list 'while '__sequence__
			    (list 'setq atom '(car __sequence__)))
		      body
		      (list '(setq __sequence__ (cdr __sequence__))))
	      (append (list 'while '(< __i__ __len__)
			    (list 'setq atom '(aref __sequence__ __i__)))
		      body
		      (list '(setq __i__ (1+ __i__)))))))


David Kågedal's avatar
David Kågedal committed
92
93
94
95
(put 'lyskom-traverse 'edebug-form-spec
     '(sexp form body))


96
97
98
;;; ======================================================================
;;; lyskom-save-excursion Does not save point and mark.
;;;
Per Cederqvist's avatar
.    
Per Cederqvist committed
99
100
101
102
103
104
105
106
107

(defmacro lyskom-save-excursion (&rest forms)
  "Save-excursion without saving point and mark."
  (list 'let (list '(__buffer__ (current-buffer)))
	(list 'unwind-protect
	      (cons 'progn
		    forms)
	      '(set-buffer __buffer__))))

David Kågedal's avatar
David Kågedal committed
108
109
(put 'lyskom-save-excursion 'edebug-form-spec t)

110
111
112
;;; ======================================================================
;;; Some useful macros to make the code more readable.
;;;
Per Cederqvist's avatar
.    
Per Cederqvist committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129

(defmacro char-in-string (char string)
  "Return t if the character CHAR is member of STRING. Otherwise return nil."
  (list 'null
	(list 'not
	      (list 'string-match
		    (list 'regexp-quote
			  (list 'char-to-string char))
		    string))))

(defmacro ++ (var)
  "Increment the variable VAR and return the value."
  (list 'setq var (list '1+ var)))

(defmacro -- (var)
  "Decrement the variable VAR and return the value."
  (list 'setq var (list '1- var)))
130

131
132
133
134
135
136
137
138
139
140
141
142
143
144
(defmacro when (expr &rest body)
  "Execute BODY if EXPR evaluates to non-nil"
  (list 'if expr (cons 'progn body)))

(put 'when lisp-indent-function 1)
(put 'when 'edebug-form-spec t)

(defmacro unless (expr &rest body)
  "Execute BODY if EXPR evaluates to non-nil"
  (append (list 'if expr nil) body))

(put 'unless lisp-indent-function 1)
(put 'unless 'edebug-form-spec t)

145
146
147
148

;;; ======================================================================
;;; Multiple blocking read from server
;;;
149
150
151
152
153

(defvar lyskom-multiple-blocking-return nil
  "Return from blocking-do-multiple")

(defun lyskom-blocking-do-multiple (call-list)
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
  (save-excursion
    (set-buffer (process-buffer (or lyskom-proc
				    lyskom-blocking-process)))
    ;; If this happens, we're in trouble
    (if lyskom-is-parsing
	(lyskom-really-serious-bug))
    
    (let ((lyskom-multiple-blocking-return 'not-yet-gotten))
      (lyskom-collect 'blocking)
      (while call-list
	(apply (intern-soft (concat "initiate-"
				    (symbol-name (car (car call-list)))))
	       'blocking nil
	       (cdr (car call-list)))
	(setq call-list (cdr call-list)))
      (lyskom-use 'blocking 'lyskom-blocking-do-multiple-1)
      (while (and (eq lyskom-multiple-blocking-return 'not-yet-gotten)
171
		  (memq (process-status lyskom-proc) '(open run))
172
173
174
175
176
177
178
179
		  (not lyskom-quit-flag))
	(accept-process-output nil lyskom-apo-timeout-s lyskom-apo-timeout-ms))
      (if lyskom-quit-flag
	  (progn
	    (setq lyskom-quit-flag nil)
	    (lyskom-insert-before-prompt (lyskom-get-string 'interrupted))
	    (signal 'quit nil)))
      lyskom-multiple-blocking-return)))
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206

(defun lyskom-blocking-do-multiple-1 (&rest data)
  (setq lyskom-multiple-blocking-return data))

(defmacro blocking-do-multiple (bind-list &rest body)
  "Bind variables according to BIND-LIST and then eval BODY.
The value of the last form in BODY is returned.
Each element in BIND-LIST is a list (SYMBOL FORM) which binds SYMBOL to
the result of the server call FORM, which is the same as used in blocking-do.
All the forms in BIND-LIST are evaluated before and symbols are bound."
  (let ((bindsym 'multiple-bind-sym)
	(index 0))
    (` (let (((, bindsym)
	      (lyskom-blocking-do-multiple
	       (list (,@ (mapcar (function (lambda (x) 
					     (` (list '(, (car (car (cdr x))))
						      (,@ (cdr (car (cdr x))))))))
			   bind-list))))))
	 (let ((,@ (mapcar (function 
			    (lambda (bpat)
			      (prog1
				  (` ((, (car bpat))
				      (elt (, bindsym) (, index))))
				(setq index (1+ index)))))
			   bind-list)))
	   (,@ body))))))

David Kågedal's avatar
David Kågedal committed
207
208
(put 'blocking-do-multiple 'edebug-form-spec
     '(sexp body))
209

David Kågedal's avatar
David Kågedal committed
210
(put 'blocking-do-multiple 'lisp-indent-function 1)
David Kågedal's avatar
David Kågedal committed
211

212

213

214
215
216
217
218
219
220
221
222
223
224
;;; ============================================================
;;; General compatibility macros and functions
;;;
;;; These are functions that may not be available in every version of
;;; GNU Emacs 19. Replacement functions are defined here.
;;;
;;; Compatibility for other Emascs version should be placed in
;;; external compatibility files so as not to sully compilation with
;;; too many compiler warnings.
;;;

225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
(lyskom-provide-macro byte-code-function-p (obj)
  (` (compiled-function-p (, obj))))

(lyskom-provide-subst internal-facep (x)
  (and (vectorp x) (= (length x) 8) (eq (aref x 0) 'face)))

(lyskom-provide-function facep (x)
  "Return t if X is a face name or an internal face vector."
  (and (or (and (fboundp 'internal-facep)
                (internal-facep x))
           (and (symbolp x) 
                (boundp 'global-face-data)
                (assq x global-face-data)))
       t))


(lyskom-provide-macro save-selected-window (&rest body)
  "Execute BODY, then select the window that was selected before BODY."
  (list 'let
        '((save-selected-window-window (selected-window)))
        (list 'unwind-protect
              (cons 'progn body)
              (list 'select-window 'save-selected-window-window))))
248
249
250
251

(if (not (fboundp 'frame-width))
    (fset 'frame-width 'screen-width))

252
253
254
255
256
257
258
259
260
261
(lyskom-provide-function match-string (num &optional string)
  "Return string of text matched by last search.
NUM specifies which parenthesized expression in the last regexp.
 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
Zero means the entire text matched by the whole regexp or whole string.
STRING should be given if the last search was by `string-match' on STRING."
  (if (match-beginning num)
      (if string
	  (substring string (match-beginning num) (match-end num))
	(buffer-substring (match-beginning num) (match-end num)))))
262

263
264
265
;;; ======================================================================
;;; These macros do magic things to the compiler to avoid gratuitous
;;; compiler warnings.
266
;;;
267

David Kågedal's avatar
David Kågedal committed
268
269
;; (eval-when-compile (defvar lyskom-expected-unresolved-functions nil))
(defvar lyskom-expected-unresolved-functions nil)
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293

(defmacro lyskom-external-function (fn)
  (` (eval-when-compile
       (setq lyskom-expected-unresolved-functions
             (cons (quote (, fn))
                   lyskom-expected-unresolved-functions)))))

(defmacro lyskom-end-of-compilation ()
  (` 
   (eval-when-compile
     (if (and (boundp 'byte-compile-unresolved-functions)
              (consp (car-safe byte-compile-unresolved-functions))
              (symbolp (car-safe (car-safe 
                                  byte-compile-unresolved-functions))))
         (mapcar (function (lambda (x)
                             (setq byte-compile-unresolved-functions
                                   (delq
                                    (assq x
                                          byte-compile-unresolved-functions)
                                    byte-compile-unresolved-functions))))
                 lyskom-expected-unresolved-functions)))))


;;; ======================================================================
294
295
296
297
;;; Definition of map-keymap that hopefully works like the one in XEmacs
;;; except that the sort-first argument is ignored.
;;;

298
(lyskom-provide-function map-keymap (fn keymap &optional sort-first)
299
  (let ((r 0))
300
301
302
303
304
305
306
    (cond ((vectorp keymap)
           (while (< r (length keymap))
             (if (aref keymap r)
                 (funcall fn r (aref keymap r)))
             (setq r (1+ r))))
          (t (mapcar (function 
                      (lambda (x)
307
308
309
			;; It might be an inherited keymap
                        (if (consp x)
			    (funcall fn (car x) (cdr x)))))
310
311
312
                     (cdr keymap))))))


David Byers's avatar
David Byers committed
313
314
315
316
317
318
319
(lyskom-provide-function set-keymap-parent (keymap new-parent)
   (let ((tail keymap))
     (while (and tail (cdr tail) (not (eq (car (cdr tail)) 'keymap)))
       (setq tail (cdr tail)))
     (if tail
         (setcdr tail new-parent))))

320
321
322
323
324
325
326
327
328
329
330
331

;;; ================================================================
;;;         Faces

(defmacro lyskom-make-face (name &rest body)
  (` (if (memq (, name) (face-list))
	 nil
       (,@ body))))

(put 'lyskom-make-face 'lisp-indent-function 1)


David Kågedal's avatar
David Kågedal committed
332
(provide 'lyskom-macros)
333

334
335
;;; Local Variables: 
;;; eval: (put 'lyskom-traverse 'lisp-indent-hook 2)
336
337
338
;;; eval: (put 'lyskom-provide-macro 'lisp-indent-hook 2)
;;; eval: (put 'lyskom-provide-function 'lisp-indent-hook 2)
;;; eval: (put 'lyskom-provide-subst 'lisp-indent-hook 2)
339
;;; end: