Skip to content
This repository was archived by the owner on Oct 15, 2025. It is now read-only.

Commit fc59e70

Browse files
author
Hörmet Yiltiz
committed
works now
1 parent 6189639 commit fc59e70

6 files changed

Lines changed: 134 additions & 59 deletions

File tree

lisp_eval/__init__.py

Lines changed: 93 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -7,100 +7,134 @@
77

88
from albert import *
99

10-
md_iid = '2.3'
11-
md_version = "1.6"
10+
md_iid = "2.3"
11+
md_version = "1.0"
1212
md_name = "S-Exp Eval"
13-
md_description = "Evaluate S-Expression via Fennel or Emacs"
13+
md_description = "Evaluate S-Expression via Fennel, Emacs, Janet, Racket or Hylang."
1414
md_license = "BSD-3"
15-
md_url = "https://github.com/albertlauncher/python/tree/main/fennel_eval"
16-
md_authors = "@manuelschneid3r"
15+
md_url = "https://github.com/albertlauncher/python/tree/main/lisp_eval/"
16+
md_authors = "@hyiltiz"
1717

18-
class Plugin(PluginInstance, TriggerQueryHandler):
1918

19+
class Plugin(PluginInstance, TriggerQueryHandler):
2020
def __init__(self):
2121
# search for a language supporting S-Exp: fennel, janet, elisp, clojure, racket
2222
# TODO: this should be a configurable option
2323
# Users should make available the executables in the system PATH
2424
lang_opts = {
25-
'janet': {
26-
'prog': 'janet',
27-
'args': ['-e', '(print {})'],
28-
'url': 'https://janet-lang.org/assets/janet-big.png'
25+
"elisp": {
26+
"prog": "emacs",
27+
"args": ["--batch", "--eval", "(print {})"],
28+
"url": "emacs-small.png",
29+
},
30+
"elisp": {
31+
"prog": "Emacs",
32+
"args": ["--batch", "--eval", "(print {})"],
33+
"url": "emacs-small.png",
34+
},
35+
"fennel": {
36+
"prog": "fennel",
37+
"args": ["-e", "(print {})"],
38+
"url": "fennel.svg",
2939
},
30-
'fennel': {
31-
'prog': 'fennel',
32-
'args': ['-e', '(print {})'],
33-
'url': 'https://janet-lang.org/assets/janet-big.png'
40+
"janet": {
41+
"prog": "janet",
42+
"args": ["-e", "(print {})"],
43+
"url": "janet.png",
3444
},
35-
'elisp': {
36-
'prog': 'emacs',
37-
'args': ['--batch', '--eval', '(print {})'],
38-
'url': 'https://www.gnu.org/software/emacs/images/emacs.png'
45+
"hylang": {
46+
"prog": "hy", # this is a pip3 dependency: `hy`
47+
"args": ["-c", "(print {})"],
48+
"url": "cuddles.png",
3949
},
40-
'racket': {
41-
'prog': 'racket',
42-
'args': ['-e', '(print {})'],
43-
'url': 'https://racket-lang.org/img/racket-logo.svg'
50+
"racket": {
51+
"prog": "racket",
52+
"args": ["-e", "(print {})"],
53+
"url": "racket.svg",
4454
},
4555
}
4656
self.lang_opts = lang_opts
4757

48-
test_sexp = '(+ 1 1)'
58+
test_sexp = "(+ 1 1)"
4959
detected_langs = []
5060
for lang, args in lang_opts.items():
51-
script = args['args'][-1].format(test_sexp)
61+
script = args["args"][-1].format(test_sexp)
62+
print(f"Testing {lang} with test script {script}")
5263
try:
53-
proc = subprocess.run([args['prog'], *args['args'][0:-1], script], input=script.encode(),
54-
stdout=subprocess.PIPE)
64+
proc = subprocess.run(
65+
[args["prog"], *args["args"][0:-1], script],
66+
input=script.encode(),
67+
stdout=subprocess.PIPE,
68+
)
5569
result = proc.stdout.strip()
5670
if result:
5771
detected_langs.append(lang)
72+
break # TODO: unless we provide alternatives in drop-down menu, do not bother detecting the rest
5873
except FileNotFoundError as ex:
59-
# TODO: does Albert has a logger?
74+
warning(str(ex))
6075
continue
6176

62-
6377
PluginInstance.__init__(self)
64-
TriggerQueryHandler.__init__(
65-
self, self.id, self.name, self.description,
66-
synopsis='<Evaluate Lisp S-Expression>',
67-
defaultTrigger='() '
68-
)
6978
self.detected_langs = detected_langs
7079
self.call_external = lang_opts[detected_langs[0]]
71-
self.iconUrls = self.call_external['url']
80+
self.iconUrls = [f"file:{Path(__file__).parent}/{self.call_external['url']}"]
81+
TriggerQueryHandler.__init__(
82+
self,
83+
self.id,
84+
self.name,
85+
self.description,
86+
synopsis=f"<Evaluate S-Expression using {detected_langs[0]}",
87+
defaultTrigger="() ",
88+
)
7289

73-
def handleTriggerQuery(self, query):
74-
act = lambda script: (
75-
lambda: runDetachedProcess([
76-
self.call_external['prog'],
77-
*self.call_external['args'][0:-1],
78-
self.call_external['args'][-1].format(script)]
90+
def runSubprocess(self, query_script):
91+
try:
92+
script = self.call_external["args"][-1].format(query_script)
93+
proc = subprocess.run(
94+
[
95+
self.call_external["prog"],
96+
*self.call_external["args"][0:-1],
97+
script,
98+
],
99+
input=script.encode(),
100+
stdout=subprocess.PIPE,
101+
stderr=subprocess.PIPE,
102+
capture_output=False,
103+
check=False,
79104
)
80-
)
105+
print(f'---------- :runSubprocess: ----------------------------------------------------')
106+
print(f'stderr: {proc.stderr}, returncode: {proc.returncode}, type: {type(proc.stderr)}')
107+
print(f'stdout: {proc.stdout}, type: {type(proc.stdout)}')
108+
print('\n\n')
109+
result = proc.stderr.decode('utf-8', errors='replace').strip() + proc.stdout.decode('utf-8', errors='replace').strip()
110+
except Exception as ex:
111+
print(f'Python Subprocess call exception: {str(ex)}')
112+
result = str(ex)
113+
return result
81114

115+
def handleTriggerQuery(self, query):
82116
stripped = query.string.strip()
83117
if stripped:
84118
try:
85-
result = act(stripped)
86-
119+
result = self.runSubprocess(stripped)
87120
except Exception as ex:
88121
result = ex
89122

90-
result_str = str(result)
91-
92-
query.add(StandardItem(
93-
id=self.id,
94-
text=result_str,
95-
subtext=type(result).__name__,
96-
inputActionText=query.trigger + result_str,
97-
iconUrls=self.iconUrls,
98-
actions = [
99-
Action("copy", "Copy result to clipboard", lambda r=result_str: setClipboardText(r)),
100-
Action("exec", "Evaluate S-Expression", lambda r=result_str: act(r)),
101-
]
102-
))
123+
result_str = result
103124

104-
# if __name__ == '__main__':
105-
# plugin = Plugin()
106-
# plugin.run()
125+
query.add(
126+
StandardItem(
127+
id=self.id,
128+
text=result_str,
129+
subtext=stripped,
130+
inputActionText=self.call_external["prog"] + ": " + result_str,
131+
iconUrls=self.iconUrls,
132+
actions=[
133+
Action(
134+
"copy",
135+
"Copy result to clipboard",
136+
lambda r=result_str: setClipboardText(r),
137+
),
138+
],
139+
)
140+
)

lisp_eval/cuddles.png

10.6 KB
Loading

lisp_eval/emacs-small.png

4.64 KB
Loading

lisp_eval/fennel.svg

Lines changed: 24 additions & 0 deletions
Loading

lisp_eval/janet.png

20.2 KB
Loading

lisp_eval/racket.svg

Lines changed: 17 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)