Renamed src/lib Parenscript library files, got rid of Parenscript CSS system.
[clinton/parenscript.git] / src / lib / ps-macro-lib.lisp
CommitLineData
97eb9b75 1(in-package :parenscript)
3eb7802d
VS
2
3;;; Handy utilities for doing common tasks found in many web browser
4;;; JavaScript implementations
5
4a987e2b 6(defpsmacro do-set-timeout ((timeout) &body body)
3eb7802d
VS
7 `(set-timeout (lambda () ,@body) ,timeout))
8
9;;; Arithmetic
10
11(defmacro def-js-maths (&rest mathdefs)
4a987e2b 12 `(progn ,@(mapcar (lambda (def) (cons 'defpsmacro def)) mathdefs)))
3eb7802d
VS
13
14(def-js-maths
15 (min (&rest nums) `(*math.min ,@nums))
16 (max (&rest nums) `(*math.max ,@nums))
31ad390e 17 (ceiling (n &optional divisor) `(*math.ceil ,(if divisor `(/ ,n ,divisor) n)))
3eb7802d
VS
18 (abs (n) `(*math.abs ,n))
19 (sin (n) `(*math.sin ,n))
20 (cos (n) `(*math.cos ,n))
21 (tan (n) `(*math.tan ,n))
22 (acos (n) `(*math.acos ,n))
23 (asin (n) `(*math.asin ,n))
24 (atan (n) `(*math.atan ,n))
25 (exp (n) `(*math.exp ,n))
31ad390e 26 (floor (n &optional divisor) `(*math.floor ,(if divisor `(/ ,n ,divisor) n)))
3eb7802d 27 (expt (base power) `(*math.pow ,base ,power))
31ad390e 28 (round (n &optional divisor) `(*math.round ,(if divisor `(/ ,n ,divisor) n)))
3eb7802d
VS
29 (random (&optional upto) (if upto
30 `(floor (* ,upto (*math.random)))
30135005
VS
31 '(*math.random)))
32 (oddp (n) `(% ,n 2))
33 (evenp (n) `(not (oddp ,n))))
854b1c7e
VS
34
35;;; Exception handling
36
4a987e2b 37(defpsmacro ignore-errors (&body body)
854b1c7e 38 `(try (progn ,@body) (:catch (e))))
2e51aff5 39
c72e87d8
VS
40;;; Data structures
41
42(defpsmacro length (a)
43 `(.size ,a))
44
2e51aff5
VS
45;;; Misc
46
47(defpsmacro null (x)
48 `(= ,x nil))
c72e87d8
VS
49
50(defpsmacro @ (obj &rest props)
51 "Handy slot-value/aref composition macro."
52 (if (null props)
53 obj
54 `(@ (slot-value
55 ,(if (stringp obj) `($ ,obj) obj)
56 ,(let ((prop (macroexpand (first props))))
57 (if (symbolp prop)
58 `',prop
59 prop)))
60 ,@(cdr props))))