reddit / emacs

Is there a standard way to use formatting expandos with more one character (e.g. "%(foo) -- [%(bar&]")?

Jack-o-tall-tales · 2022-04-23 12:50
I've run into this issue in a few recent elisp experiments. I want to use format strings with custom expandos to represent points of key-value data (that is, I want to print a formatted string from a object which has keys and values, like a bibtex entry). Normally I could do this with `format-spec`, and associate certain keys in the original object with certain expando letters. But often the data I am formatting either has *many* fields (more than 26, so a format-spec gets unwieldy), or has a few core fields common to all data points, but could have any arbitrary fields beyond those (so it is impossible to assign expandos to all field names). Biblatex entries for example have a reference key, 133 defined field names, and facility for users to define and use whichever fields they want! Even expandos for a-z, A-Z and 0-9 wouldn't cover all that. On the assumption that the field names are always unique within a single object, a good solution to this might be somthing like `format "%(field-name-1) -- [%(field-name-2):%(field-name-3)]"`, so as to print something like `foo -- [a:b]`. The idea is that the expando names (`field-name-1`, `field-name-2`, etc.) are programmatically checked against the entry (or against a list generated programatically *from* the entry), so there is no need for an explicit list of expandos like `format-spec` uses. I think I have seen a few packages do things like this before, but can't remember exactly where. Is there a standard way to do this? TIA!
wasamasa · 2022-04-23 20:16
I'm not aware of any standard solution, but there's at least two external libraries solving this: - s.el with `s-format` - lui-format.el with `lui-format`
Jack-o-tall-tales · 2022-04-23 20:44
Thank you so much! Having looked at the options think I'll go with s.el. `s-format` looks *really* powerful.