store/[org.clojure/clojure "1.8.0"] clj::clojure.core/defmacro

Official Documentation

Add notes
Arities
==================================================
   [name doc-string? attr-map? [params*] body]
   [name doc-string? attr-map? ([params*] body) + attr-map?]

Docstring
==================================================
  Like defn, but the resulting function name is declared as a
  macro and will be used as a macro by the compiler when it is
  called.

Source

(def

 ^{:doc "Like defn, but the resulting function name is declared as a
  macro and will be used as a macro by the compiler when it is
  called."
   :arglists '([name doc-string? attr-map? [params*] body]
                 [name doc-string? attr-map? ([params*] body)+ attr-map?])
   :added "1.0"}
 defmacro (fn [&form &env 
                name & args]
             (let [prefix (loop [p (list name) args args]
                            (let [f (first args)]
                              (if (string? f)
                                (recur (cons f p) (next args))
                                (if (map? f)
                                  (recur (cons f p) (next args))
                                  p))))
                   fdecl (loop [fd args]
                           (if (string? (first fd))
                             (recur (next fd))
                             (if (map? (first fd))
                               (recur (next fd))
                               fd)))
                   fdecl (if (vector? (first fdecl))
                           (list fdecl)
                           fdecl)
                   add-implicit-args (fn [fd]
                             (let [args (first fd)]
                               (cons (vec (cons '&form (cons '&env args))) (next fd))))
                   add-args (fn [acc ds]
                              (if (nil? ds)
                                acc
                                (let [d (first ds)]
                                  (if (map? d)
                                    (conj acc d)
                                    (recur (conj acc (add-implicit-args d)) (next ds))))))
                   fdecl (seq (add-args [] fdecl))
                   decl (loop [p prefix d fdecl]
                          (if p
                            (recur (next p) (cons (first p) d))
                            d))]
               (list 'do
                     (cons `defn decl)
                     (list '. (list 'var name) '(setMacro))
                     (list 'var name)))))

Example 1

Edit
(def dbg 1)

(defmacro chk-flagM
  "Throws an exception if flag does not resolve; else returns flag's value."
  [flag]
  (if (not (resolve flag))
    (throw (Exception. (str 'flag " is not a valid var.")))
    flag))

(defn write-csv-file
  "Writes a csv file using a key and an s-o-s"
  [out-sos out-file]

  (if (>= (chk-flagM dbg) 2)
    (println (first out-sos), "\n", out-file))

  (spit out-file "" :append false)
  (with-open [out-data (io/writer out-file)]
    (csv/write-csv out-data (map #(concat % [""]) out-sos))))

Example 2

Edit
(defmacro with-tree [tree & body]
  "works on a JTree and restores its expanded paths after executing body"
  `(let [tree# ~tree
         root# (.getRoot (.getModel tree#))
         expanded# (if-let [x# (.getExpandedDescendants
                                tree# (TreePath. root#))]
                     (enumeration-seq x#)
                     ())
         selectionpaths# (. selectionmodel# getSelectionPaths)]
     ~@body
     (doseq [path# expanded#]
       (.expandPath tree# path#))))

;; usage:

(with-tree *one-jtree-instance*
   ;; some code here...
  )

Example 3

Edit
(defmacro unless [pred a b]
  `(if (not ~pred) ~a ~b))

;; usage:
(unless false
        (println "Will print")
        (println "Will not print"))

Uses on crossclj