tags:

views:

20

answers:

2

I have this data structure (basically):

(setq ssm-list '(tasklist
  ((id . "10525295")
   (name . "Inbox")
   (sort_order . "0"))))

This works for getting the name:

(defun ssm-list-get-prop (list prop)
  (cdr (assoc prop (car (cdr list)))))

(ssm-list-get-prop slack-one-list 'name)

What'd I like is to create a macro that will create a defun with the name ssm-list-name (or ssm-list-id) as there are actually a lot more properties in the list.

So I tried this:

(defmacro ssm-list-prop-defun (field)
  `(defun ,(intern (concat "ssm-list-" field))
      (one-list)
      (cdr (assoc ,field (car (cdr one-list))))))

(ssm-list-prop-defun 'name)
(ssm-list-prop-defun 'id)

But the last two calls failed miserably with (wrong-type-argument characterp quote) I tried putting symbol-name in the macro but that didn't help.

Is there a way to accomplish this?

A: 

I think you want to read about defstruct in the cl package: (info "(cl) Structures") (or http://www.gnu.org/software/emacs/manual/html_node/cl/Structures.html#Structures)

offby1
Thanks for the reply. I have looked at defstruct (and I agree with you). Unfortunately, the data structure that I'm using is from a 3rd party library and I'd rather not rewrite that too.
seth
+1  A: 

You're very close, minor edits gets you a working solution. The problem is that you're mixing symbols and strings. This will work:

(defmacro ssm-list-prop-defun (field)
                   ;; note that concat operates on strings
  `(defun ,(intern (concat "ssm-list-" field))
     (one-list)
          ;; note that you need a symbol here, so quote the 
          ;; result of the call to intern
          ;; and, if you're always using symbols, 
          ;; might as well use assq
     (cdr (assq ',(intern field) (car (cdr one-list))))))

;; pass in a string
(ssm-list-prop-defun "name")

And here's the variant that uses a symbol:

;; variant that works off a symbol
(defmacro ssm-list-prop-defun (field)
  `(defun ,(intern (concat "ssm-list-" (symbol-name field)))
     (one-list)
     (cdr (assq ',field (car (cdr one-list))))))

(ssm-list-prop-defun name)
Trey Jackson
That works. thanks. So, there's no way to pass in a symbol to (e.g. (ssm-list-prop-defun 'name)?
seth
@seth Sure, I've added that variant.
Trey Jackson
Thanks! I actually tried that variant but called it with 'name instead of a bare name.
seth