Skip to content

Instantly share code, notes, and snippets.

@osmatsuda
Last active September 30, 2016 00:28
Show Gist options
  • Save osmatsuda/6eb57634ebd159b06ce699dee999dc48 to your computer and use it in GitHub Desktop.
Save osmatsuda/6eb57634ebd159b06ce699dee999dc48 to your computer and use it in GitHub Desktop.
(defmacro calc-eval-format (expr-str &rest vars)
(let ((expr (cl-gensym))
(result (cl-gensym)))
`(let ((case-fold-search nil))
(setq ,expr ,expr-str)
(cl-loop for sym in '(,@vars) by #'cl-cdddr
for eq-sym in (cdr '(,@vars)) by #'cl-cdddr
for val in (cddr '(,@vars)) by #'cl-cdddr
do
(unless (and (symbolp sym)
(numberp val)
(eq '= eq-sym))
(error "Wrong format of variables"))
(setq ,expr (replace-regexp-in-string
(concat "\\<" (symbol-name sym) "\\>")
(number-to-string val)
,expr t)))
(setq ,result (calc-eval ,expr))
(if (stringp ,result)
(kill-new ,result)
(if (listp ,result)
(kill-new (cadr ,result))
,result)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment