Skip to content

Commit f9f2275

Browse files
committed
A few additions
1 parent 46e9b27 commit f9f2275

2 files changed

Lines changed: 73 additions & 6 deletions

File tree

emacs/elisp/ast.py

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66

77
__all__ = ['ElispAstNode', 'Form', 'Literal', 'Symbol', 'Cons', 'List', 'Quote',
8-
'Raw', 'to_elisp', 'make_list', 'symbols', 'quote', 'cons']
8+
'Raw', 'to_elisp', 'make_list', 'make_alist', 'make_plist',
9+
'symbols', 'quote', 'cons']
910

1011

1112
class ElispAstNode:
@@ -14,6 +15,10 @@ class ElispAstNode:
1415
def __repr__(self):
1516
return '<el %s>' % self
1617

18+
def _quoted(self):
19+
"""Get representation within a quote form."""
20+
return str(self)
21+
1722

1823
class Form(ElispAstNode):
1924
"""Pretty much everything is a form, right?"""
@@ -97,7 +102,10 @@ def __eq__(self, other):
97102
and other.cdr == self.cdr
98103

99104
def __str__(self):
100-
return '(%s . %s)' % (self.car, self.cdr)
105+
return '(cons %s %s)' % (self.car, self.cdr)
106+
107+
def _quoted(self):
108+
return '(%s . %s)' % (self.car._quoted(), self.cdr._quoted())
101109

102110

103111
class List(Form):
@@ -118,6 +126,9 @@ def __eq__(self, other):
118126
def __str__(self):
119127
return '(%s)' % ' '.join(map(str, self.items))
120128

129+
def _quoted(self):
130+
return '(%s)' % ' '.join(item._quoted() for item in self.items)
131+
121132

122133
class Quote(Form):
123134
"""A quoted Elisp form.
@@ -135,7 +146,7 @@ def __eq__(self, other):
135146
return isinstance(other, Quote) and other.form == self.form
136147

137148
def __str__(self):
138-
return "'%s" % self.form
149+
return "'%s" % self.form._quoted()
139150

140151

141152
class Raw(ElispAstNode):
@@ -236,9 +247,16 @@ def cons(car, cds):
236247
return Cons(to_elisp(car), to_elisp(cds))
237248

238249

239-
def symbols(*names):
250+
def symbols(*names, quote=False):
240251
"""Create a list of symbols.
241252
253+
Parameters
254+
----------
255+
names
256+
Symbol names.
257+
quote : bool
258+
Quote the resulting list.
259+
242260
Returns
243261
-------
244262
.List
@@ -254,8 +272,57 @@ def symbols(*names):
254272
else:
255273
raise TypeError('Expected str or Symbol, got %s' % type(name).__name__)
256274

257-
return List(s)
275+
l = List(s)
276+
return Quote(l) if quote else l
277+
278+
279+
def _convert_pairs(pairs):
280+
if isinstance(pairs, dict):
281+
pairs = pairs.items()
282+
283+
l = []
284+
285+
for key, value in pairs:
286+
key = Symbol(key) if isinstance(key, str) else to_elisp(key)
287+
l.append((key, to_elisp(value)))
288+
289+
return l
290+
291+
292+
def make_alist(pairs, quote=False):
293+
"""Create an alist from a set of key-value pairs.
294+
295+
Parameters
296+
----------
297+
pairs
298+
Key-value pairs as a dict or collections of 2-tuples.
299+
quote : bool
300+
Quote the resulting form.
301+
302+
Returns
303+
-------
304+
ElispAstNode
305+
"""
306+
alist = List([cons(key, value) for key, value in _convert_pairs(pairs)])
307+
return Quote(alist) if quote else alist
308+
258309

310+
def make_plist(pairs, quote=False):
311+
"""Create a plist from a set of key-value pairs.
312+
313+
Parameters
314+
----------
315+
pairs
316+
Key-value pairs as a dict or collections of 2-tuples.
317+
quote : bool
318+
Quote the resulting form.
319+
320+
Returns
321+
-------
322+
ElispAstNode
323+
"""
324+
plist = List([x for kv in _convert_pairs(pairs) for x in kv])
325+
return Quote(plist) if quote else plist
259326

260327

261328
def print_elisp_string(string):

emacs/emacs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def client(cls, args=(), **kwargs):
136136
Parameters
137137
----------
138138
args : tuple
139-
Extra arguments to pass the ``emacs`` command.
139+
Extra arguments to pass the ``emacsclient`` command.
140140
141141
Returns
142142
-------

0 commit comments

Comments
 (0)