Search code examples
emacsmacroselisp

Elisp: how to make this function short


I am implementing an plugin, which a lot of project types are remembered. For example: angular, meteor, ember, rails ...

And keyed arguments are used. The function creates an hash table with provided keys and values, and assign the hash table to another hash table.

Code below:

    (defun jst-remember-project-type (type &rest args)
      "Let JST remember another project type."
      (let (label value testing-framework spec-dir config-file source-dir
                  command-ci command-browser spec-to-target target-to-spec
                  dominating-files browser-url table)
        (while (not (= 0 (length args)))
          (setq label (pop args))
          (setq value (pop args))
          (and (equal :testing-framework label) (setq testing-framework value))
          (and (equal :spec-dir label) (setq spec-dir value))
          (and (equal :source-dir label) (setq source-dir value))
          (and (equal :config-file label) (setq config-file value))
          (and (equal :command-ci label) (setq command-ci value))
          (and (equal :command-browser label) (setq command-browser value))
          (and (equal :browser-url label) (setq browser-url value))
          (and (equal :spec-to-target label) (setq spec-to-target value))
          (and (equal :target-to-spec label) (setq target-to-spec value))
          (and (equal :dominating-files label) (setq dominating-files value)))
        (if (gethash type jst-known-project-types)
            (error "Redefined JST project type.")
          (setq table (make-hash-table :test 'equal))
          (puthash :testing-framework testing-framework table)
          (puthash :spec-dir spec-dir table)
          (puthash :config-file config-file table)
          (puthash :source-dir spec-dir table)
          (puthash :command-ci command-ci table)
          (puthash :command-browser command-browser table)
          (puthash :spec-to-target spec-to-target table)
          (puthash :target-to-spec target-to-spec table)
          (puthash :dominating-files dominating-files table)
          (puthash :browser-url browser-url table)
          (puthash type table jst-known-project-types))) nil)

And I have a lot of redundant functions like this. I want these kind of functions automatically generated by a macro.

Actually, only a list of keys and a table is needed. Everything else can be generated. However I don't know how to write the macro.

    (defmacro jst-remember-keyed (keys table)
      "This macro helps with jst-remember functions."
      (let (label value QUESTION HERE keys)))

How to convert variable from :symbol and convert :symbol from variable easily?

    (make-symbol "fuck")
    fuck ;; Error occurs

    (let (((make-symbol "fuck") "Diao"))
      (message fuck)
      ) ;; Error occurs

Thanks a lot!


Solution

  • Untested:

    (require 'cl)
    (defun jst-remember-project-type (type &rest args)
      "Let JST remember another project type."
      (if (gethash type jst-known-project-types)
          (error "Redefined JST project type.""")
        (let ((table (make-hash-table :test #'equal)))
          (loop for (label value) on args by #'cddr
                do (puthash label value table))
          (puthash type table jst-known-project-types))))