Skip to content

Commit

Permalink
magnars#27 recoded with s-match-strings-all
Browse files Browse the repository at this point in the history
  • Loading branch information
nicferrier committed May 9, 2013
1 parent b3a79dd commit 1425033
Showing 1 changed file with 15 additions and 33 deletions.
48 changes: 15 additions & 33 deletions s.el
Original file line number Diff line number Diff line change
Expand Up @@ -486,17 +486,19 @@ transformation."
`s-lex-format' inserts values with (format \"%S\").")

(defvar s-lex-value-when-dynamic :symbol-value
"What to do in `s-lex-format' when not lexical.
This variables can be in one of the following states:
`:error' - using the macro causes an error to be signalled
`:symbol-value' - uses `symbol-value' to look up the dynamic value
`t' - uses the name of the variable reference
Let bind this variable to change the behaviour of
`s-lex-format'.")
(defun s-lex-fmt|expand (fmt)
"Expand FMT into lisp."
(list 's-format fmt (quote 'aget)
(append '(list)
(mapcar
(lambda (matches)
(list
'cons
(cadr matches)
`(format
(if s-lex-value-as-lisp "%S" "%s")
,(intern (cadr matches)))))
(s-match-strings-all "${\\([^}]+\\)}" fmt)))))

(defmacro s-lex-format (format-str)
"`s-format' with the lexical environment.
Expand All @@ -509,28 +511,8 @@ any lexical variable:
The values of the lexical variables are interpolated with \"%s\"
unless the variable `s-lex-value-as-lisp' is `t' and then they
are interpolated with \"%S\".
If the macro is used in a non-lexical-binding context then it's
behaviour depends on the variable `'"
(let ((pv (make-symbol "pv")))
`(let ((,pv (lambda ())))
(s-format
,format-str
(lambda (var-name)
(let ((value
(if (eq 'closure (car ,pv))
(let ((value
(assoc (intern var-name) (cadr ,pv))))
(when value (cdr value)))
(case s-lex-value-when-dynamic
(:error (error "not in a lexical environment"))
(:symbol-value (symbol-value
(intern var-name)))
(t var-name)))))
(if s-lex-value-as-lisp
(format "%S" value)
(format "%s" value))))))))
are interpolated with \"%S\"."
(s-lex-fmt|expand format-str))

(provide 's)
;;; s.el ends here

0 comments on commit 1425033

Please sign in to comment.