macros.el 6.74 KB
Newer Older
Linus Tolke's avatar
Linus Tolke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
;;;;;
;;;;; $Id$
;;;;; Copyright (C) 1991  Lysator Academic Computer Association.
;;;;;
;;;;; 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 
;;;;; the Free Software Foundation; either version 1, 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. 
;;;;;
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
37
(defconst lyskom-clientversion-long "$Id$\n"
  "Version for every file in the client.")


Per Cederqvist's avatar
.    
Per Cederqvist committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

;;; lyskom-traverse - traverse a sequence.

(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
59
60
61
62
(put 'lyskom-traverse 'edebug-form-spec
     '(sexp form body))


Per Cederqvist's avatar
.    
Per Cederqvist committed
63
64
65
66
67
68
69
70
71
72
;;;; lyskom-save-excursion Does not save point and mark.

(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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
;; The following is equal to this:
;; (eval-when-compile (require 'edebug))
;; (def-edebug-spec lyskom-save-excursion t)
(put 'lyskom-save-excursion 'edebug-form-spec t)

;;;; LysKOM user commands

;; The new, blocking commands have a very similar structure
;;
;;  (defun kom-cmd (args)
;;    "Documentation"
;;    (interactive "...")
;;    (lyskom-start-of-command 'kom-cmd)
;;    (unwind-protect
;;        (progn ...)
;;      (lyskom-end-of-command)))
;;
;; This can now be written as
;;
;; (def-kom-command kom-cmd (args)
;;   "Documentation"
;;   (interactive "...")
;;   ...)


(defmacro def-kom-command (cmd args doc interactive-decl &rest forms)
  (list 'defun cmd args doc interactive-decl
	(list 'lyskom-start-of-command (list 'quote cmd))
	(list 'unwind-protect
	      (cons 'progn
		    forms)
	      (list 'lyskom-end-of-command))))


;;(def-edebug-spec def-kom-command
;;  (&define name lambda-list
;;                [&optional stringp]   ; Match the doc string, if present.
;;                ("interactive" interactive)
;;                def-body))
(put 'def-kom-command 'edebug-form-spec
     '(&define name lambda-list
	       [&optional stringp]	; Match the doc string, if present.
	       ("interactive" interactive)
	       def-body))

Per Cederqvist's avatar
.    
Per Cederqvist committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

;;;; Some useful macros to make the code more readable.

(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)))
137

David Kågedal's avatar
David Kågedal committed
138
(defmacro when (expr &rest body)
David Kågedal's avatar
David Kågedal committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
  "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)

(defsubst listify-vector (vector)
  "Turn VECTOR into a list"
  (append vector nil))

(defun reverse-assoc (key cache)
  "Same as assoc, but searches on last element in a list"
  (reverse (assoc key (mapcar (function reverse) cache))))

(defun skip-first-zeros (list)
  (while (and list (zerop (car list)))
    (setq list (cdr list)))
  list)
157

158
159
;; Multiple blocking read from server

160
161
162
163
(defvar lyskom-quit-flag nil
  "A flag indicating if the filter was interrupted by C-g.
 It is set to the same value as quit-flag on filter exit.")

164
165
166
167
168
169
170
171
172
173
174
175
176
(defvar lyskom-multiple-blocking-return nil
  "Return from blocking-do-multiple")

(defun lyskom-blocking-do-multiple (call-list)
  (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)
David Byers's avatar
David Byers committed
177
178
    (while (and (eq lyskom-multiple-blocking-return 'not-yet-gotten)
		(not lyskom-quit-flag))
David Byers's avatar
David Byers committed
179
      (accept-process-output nil lyskom-apo-timeout-s lyskom-apo-timeout-ms))
180
181
182
183
184
    (if lyskom-quit-flag
	(progn
	  (setq lyskom-quit-flag nil)
	  (lyskom-insert-before-prompt (lyskom-get-string 'interrupted))
	  (signal 'quit nil)))
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
    lyskom-multiple-blocking-return))

(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
213
214
(put 'blocking-do-multiple 'edebug-form-spec
     '(sexp body))
215

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

218
219
220
;;; Local Variables: 
;;; eval: (put 'lyskom-traverse 'lisp-indent-hook 2)
;;; end: