Sprucial
Sprucial

Reputation: 228

Creating a keyword symbol

I'm trying to create a macro that takes some keyword parameters and, only if the parameter is defined, add entries to a list. Splitting this up the following code should illustrate what I need.

(defun add-if-not-null (var lst)
  (if (not (null var))
      (append (cons (***) (cons ver '())) lst)))

The three asterisks show the part I'm trying to figure out. This basically takes a symbol name and turns it into a keyword representation. e.g. width converts to :width

(let ((width 100))
    (add-if-not-null (width '())))

Should return

(:width 100)

I'm using cl-who to create an svg representation and basically want to set attributes like width and height only if they are specified as parameters to my macro that wraps the document.

Upvotes: 2

Views: 1157

Answers (1)

Rainer Joswig
Rainer Joswig

Reputation: 139251

The name of the package is KEYWORD. Symbols are created (if necessary) and put into a package using INTERN.

CL-USER 11 > (intern (symbol-name 'width) "KEYWORD")
:WIDTH

A macro:

CL-USER 29 > (defmacro add-if-not-null (var list)
               (check-type var symbol)
               `(when var
                  (push (list ,(intern (symbol-name var) "KEYWORD") var)
                        ,list)))
ADD-IF-NOT-NULL

CL-USER 30 > (macroexpand-1 '(add-if-not-null width some-list))
(WHEN VAR (PUSH (LIST :WIDTH VAR) SOME-LIST))
T

Upvotes: 4

Related Questions