Advertisement
Shinmera

lQuery Syntax Macro (with comments)

Jun 18th, 2013
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Define a macro called "$" with one argument "selector-or-nodes" and an arbitrary number of extra
  2. ; arguments that are listed in "modifiers".
  3. (defmacro $ (selector-or-nodes &rest modifiers)
  4.   (if (not selector-or-nodes)
  5.       `*LQUERY-MASTER-DOCUMENT*                                                     ;In case selector-or-nodes is NIL, simply return the entire document.
  6.       `(let ((working-nodes *LQUERY-MASTER-DOCUMENT*))                              ;Create a variable "working-nodes" and set it to the document.
  7.          ,(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.
  8.               `(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.
  9.               `(setf working-nodes ,selector-or-nodes))                             ;Otherwise simply set working-nodes to the value specified.
  10.          
  11.          (if (not (listp working-nodes))                                            ;Safety check to make sure we can only process lists.
  12.              (setf working-nodes (list working-nodes)))
  13.          
  14.          ,(if modifiers                                                             ;If any modifiers were supplied,
  15.               `(progn                                                               ;Do the following
  16.                  ,@(loop for mod in modifiers collect                               ;This loop "generates" function calls that will be executed later on when the program is actually run.
  17.                         `(setf working-nodes                                        ;Set "working-nodes" to the following new value
  18.                                (,(find-symbol (mkstr 'nodefun- (car mod)) :lquery)  ;Generated by the function we passed into the macro as a "modifier" argument.
  19.                                     working-nodes                                   ;Pass the current "working-nodes" into this function
  20.                                     ,@(cdr mod))))))                                ;As well as all the other arguments supplied to the function call.
  21.          
  22.          working-nodes)))                                                           ;Finally return "working-nodes".
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement