Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ; Define a macro called "$" with one argument "selector-or-nodes" and an arbitrary number of extra
- ; arguments that are listed in "modifiers".
- (defmacro $ (selector-or-nodes &rest modifiers)
- (if (not selector-or-nodes)
- `*LQUERY-MASTER-DOCUMENT* ;In case selector-or-nodes is NIL, simply return the entire document.
- `(let ((working-nodes *LQUERY-MASTER-DOCUMENT*)) ;Create a variable "working-nodes" and set it to the document.
- ,(if (stringp selector-or-nodes) ;In case "selector-or-nodes" is a string, we assume the user wants to use it as a CSS selector.
- `(setf working-nodes (css:query ,selector-or-nodes working-nodes)) ;Use the css:query function to get the desired elements and set "working-nodes" to that.
- `(setf working-nodes ,selector-or-nodes)) ;Otherwise simply set working-nodes to the value specified.
- (if (not (listp working-nodes)) ;Safety check to make sure we can only process lists.
- (setf working-nodes (list working-nodes)))
- ,(if modifiers ;If any modifiers were supplied,
- `(progn ;Do the following
- ,@(loop for mod in modifiers collect ;This loop "generates" function calls that will be executed later on when the program is actually run.
- `(setf working-nodes ;Set "working-nodes" to the following new value
- (,(find-symbol (mkstr 'nodefun- (car mod)) :lquery) ;Generated by the function we passed into the macro as a "modifier" argument.
- working-nodes ;Pass the current "working-nodes" into this function
- ,@(cdr mod)))))) ;As well as all the other arguments supplied to the function call.
- working-nodes))) ;Finally return "working-nodes".
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement