Comments (9)
I think that looks brilliant. But maybe as a separate function, so you can ditch the 'lex-value all together?
(let ((x 1))
(s-lex-format "x is ${x}"))
from s.el.
The only trouble with that is that I think there are two things that
would be useful, lexical access and lexical access with Lisp output:
I'm using this code right now:
(defvar lexical-value-when-dynamic :error
"How `lexical-value' should act in dynamic scope.
Valid values are `:error', which is the default and causes
`lexical-value' to error when called in dynamic scope, or
`:symbol-value' which causes the `symbol-value' function to be
called for the symbol or `nil' which causes nil to be returned.")
(defmacro lexical-value (lexical-symbol)
"What is the value of the LEXICAL-SYMBOL specified?"
(let ((pv (make-symbol "pv"))
(sv (make-symbol "snv")))
`(let ((,pv (lambda ())))
(if (eq 'closure (car ,pv))
(let ((value
(assoc ,lexical-symbol (cadr ,pv))))
(when value (cdr value)))
(case lexical-value-when-dynamic
(:error (error "not in a lexical environment"))
(:symbol-value (symbol-value ,lexical-symbol))
(t nil))))))
(defmacro with-sformat-lex (&rest body)
(declare (debug (&rest form))
(indent 0))
`(flet ((lex-val (var)
(let ((lexical-value-when-dynamic :symbol-value)
(variable (intern var)))
(lexical-value variable)))
(lisp-val (var) (format "%S" (lex-val var))))
,@body))
So I can go:
(let ((x 1)
(y '(a b c)))
(with-sformat-lex
(format "x is: ${x} and y is: ${y}" 'lisp-val)))
or I could use lex-val.
I guess I could start adding more dynamic options to format to control
how things are output?
from s.el.
What I want are fewer dynamic options and instead a cleaner API. Functions
that do one thing.
On Mon, Apr 29, 2013 at 3:32 PM, Nic Ferrier [email protected]:
The only trouble with that is that I think there are two things that
would be useful, lexical access and lexical access with Lisp output:I'm using this code right now:
(defvar lexical-value-when-dynamic :error "How `lexical-value' should act in dynamic scope. Valid values are `:error', which is the default and causes `lexical-value' to error when called in dynamic scope, or `:symbol-value' which causes the `symbol-value' function to be called for the symbol or `nil' which causes nil to be returned.") (defmacro lexical-value (lexical-symbol) "What is the value of the LEXICAL-SYMBOL specified?" (let ((pv (make-symbol "pv")) (sv (make-symbol "snv"))) `(let ((,pv (lambda ()))) (if (eq 'closure (car ,pv)) (let ((value (assoc ,lexical-symbol (cadr ,pv)))) (when value (cdr value))) (case lexical-value-when-dynamic (:error (error "not in a lexical environment")) (:symbol-value (symbol-value ,lexical-symbol)) (t nil)))))) (defmacro with-sformat-lex (&rest body) (declare (debug (&rest form)) (indent 0)) `(flet ((lex-val (var) (let ((lexical-value-when-dynamic :symbol-value) (variable (intern var))) (lexical-value variable))) (lisp-val (var) (format "%S" (lex-val var)))) ,@body))
So I can go:
(let ((x 1) (y '(a b c))) (with-sformat-lex (format "x is: ${x} and y is: ${y}" 'lisp-val)))
or I could use lex-val.
I guess I could start adding more dynamic options to format to control
how things are output?—
Reply to this email directly or view it on GitHubhttps://github.com//issues/27#issuecomment-17166575
.
from s.el.
Do you? I like all that being able to change the behaviour of things by let binding and such. It's fun.
from s.el.
How about this:
(defmacro s-lex-format (format-str)
(let ((pv (make-symbol "pv")))
`(s-format
,format-str
(let ((,pv (lambda ())))
(if (eq 'closure (car ,pv))
(lambda (var-name)
(let ((value
(assoc (intern var-name) (cadr ,pv))))
(when value (cdr value))))
(lambda (var-name)
(case lexical-value-when-dynamic
(:error (error "not in a lexical environment"))
(:symbol-value (symbol-value
(intern var-name)))
(t nil))))))))
(let ((lexical-value-when-dynamic :symbol-value)
(x "1"))
(s-lex-format "${x}"))
My only concern with this is that often you want the lex value to be lisp printed ("%S" instead of "%s") and that's not possible here. It could be done with a switch or a dyn var.
from s.el.
It's too convoluted for me. I would be a happy man if I could just have a formatting function that grabs content off the current scope. But come to think of it, maybe that's just concat
, with " "
separating the keys instead of ${}
. How is ${}
any better?
from s.el.
For all the reasons that s-format is better. My use case is this:
;;; ${name}.el --- ${desc}
${copy}
${decl}
;;; Commentary:
${commentary}
;;; Code:
${defn-code}
${prvide-code}
;;; ${name}.el ends here
Code that with concat and see how happy you are :-)
from s.el.
Haha, you're right. That would be terrible. :-)
On Mon, Apr 29, 2013 at 11:18 PM, Nic Ferrier [email protected]:
For all the reasons that s-format is better. My use case is this:
;;; ${name}.el --- ${desc}
${copy}
${decl}
;;; Commentary:
${commentary}
;;; Code:
${defn-code}
${prvide-code}
;;; ${name}.el ends hereCode that with concat and see how happy you are :-)
—
Reply to this email directly or view it on GitHubhttps://github.com//issues/27#issuecomment-17195146
.
from s.el.
It seems like a standard templating problem to me. And I've never really got how we can fix that with simple Lisp. Maybe there is a better solution but I'm not aware of it.
from s.el.
Related Issues (20)
- Speed Counting Matches: Unexpected Results
- Will s.el drop the support of Emacs 23? HOT 8
- Time for a new melpa-stable release? HOT 7
- Inclusion in Emacs HOT 12
- s-replace-all case-issue HOT 3
- Marmalade is dead, and the document should be updated correspondingly. HOT 1
- [feature request] function to split string into cons cell HOT 5
- Cosmetic change to `s-trim-right`
- Wildcard match? HOT 1
- Is it dead... HOT 10
- Functions which use `save-match-data` `with-temp-buffer` - side-effect-free? HOT 4
- (s-unindent s &optional bol-char) HOT 4
- 🐣 [REQUEST] - s-url-encode / s-url-decode HOT 10
- 🪲 [BUG] - Under CI 26.{1..3} fail due to no marker in buffer HOT 2
- 🪲 [BUG] - create-docs.sh failing HOT 2
- 🪲 [BUG] - Emacs 24.4 & 24.5 fail tests on CI. HOT 3
- 🐣 [REQUEST] - add s-truncate-width HOT 3
- Use ETD as a development dependency for example/tests/docs
- RFC : Dropping support for Emacs versions below 26. HOT 4
- 🐣 [REQUEST] - Add a way to check the style of given string HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from s.el.