forked from DisposaBoy/GoSublime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gs9o.py
765 lines (634 loc) · 17.8 KB
/
gs9o.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
from gosubl import about
from gosubl import gs
from gosubl import gsshell
from gosubl import mg9
from gosubl import sh
import datetime
import json
import os
import re
import shlex
import string
import sublime
import sublime_plugin
import uuid
import webbrowser
DOMAIN = "9o"
AC_OPTS = sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS
SPLIT_FN_POS_PAT = re.compile(r'(.+?)(?:[:](\d+))?(?:[:](\d+))?$')
URL_SCHEME_PAT = re.compile(r'^[\w.+-]+://')
URL_PATH_PAT = re.compile(r'^(?:[\w.+-]+://|(?:www|(?:\w+\.)*(?:golang|pkgdoc|gosublime)\.org))')
HIST_EXPAND_PAT = re.compile(r'^(\^+)\s*(\d+)$')
HOURGLASS = u'\u231B'
DEFAULT_COMMANDS = [
'help',
'run',
'build',
'replay',
'clear',
'tskill',
'tskill replay',
'tskill go',
'go',
'go build',
'go clean',
'go doc',
'go env',
'go fix',
'go fmt',
'go get',
'go install',
'go list',
'go run',
'go test',
'go tool',
'go version',
'go vet',
'go help',
'settings',
'env',
'share',
'hist',
'hist erase',
'cd',
]
DEFAULT_CL = [(s, s+' ') for s in DEFAULT_COMMANDS]
stash = {}
tid_alias = {}
def active_wd(win=None):
_, v = gs.win_view(win=win)
return gs.basedir_or_cwd(v.file_name() if v else '')
def _hkey(wd):
name = gs.setting("9o_instance")
if name:
wd = name
return '9o.hist.%s' % wd
def _wdid(wd):
name = gs.setting("9o_instance")
if name:
return name
return '9o://%s' % wd
class EV(sublime_plugin.EventListener):
def on_query_completions(self, view, prefix, locations):
pos = gs.sel(view).begin()
if view.score_selector(pos, 'text.9o') == 0:
return []
cl = set()
hkey = _hkey(view.settings().get('9o.wd', ''))
cl.update((k, k+' ') for k in gs.dval(gs.aso().get(hkey), []))
cl.update((k, k+' ') for k in aliases())
cl.update((k, k+' ') for k in builtins())
cl.update(DEFAULT_CL)
return ([cl_esc(e) for e in sorted(cl)], AC_OPTS)
def cl_esc(e):
return (e[0], e[1].replace('$', '\\$'))
class Gs9oBuildCommand(sublime_plugin.WindowCommand):
def is_enabled(self):
view = gs.active_valid_go_view(self.window)
return view is not None
def run(self):
view = self.window.active_view()
args = {'run': gs.setting('build_command', ['^1'])} if gs.is_pkg_view(view) else {}
view.run_command('gs9o_open', args)
class Gs9oInsertLineCommand(sublime_plugin.TextCommand):
def run(self, edit, after=True):
insln = lambda: self.view.insert(edit, gs.sel(self.view).begin(), "\n")
if after:
self.view.run_command("move_to", {"to": "hardeol"})
insln()
else:
self.view.run_command("move_to", {"to": "hardbol"})
insln()
self.view.run_command("move", {"by": "lines", "forward": False})
class Gs9oMoveHist(sublime_plugin.TextCommand):
def run(self, edit, up):
view = self.view
pos = gs.sel(view).begin()
if view.score_selector(pos, 'prompt.9o') <= 0:
return
aso = gs.aso()
vs = view.settings()
wd = vs.get('9o.wd')
hkey = _hkey(wd)
hist = [s for s in gs.dval(aso.get(hkey), []) if s.strip()]
if not hist:
return
r = view.extract_scope(pos)
cmd = view.substr(r).strip('#').strip()
try:
idx = hist.index(cmd) + (-1 if up else 1)
found = True
except Exception:
idx = -1
found = False
if cmd and not found:
hist.append(cmd)
aso.set(hkey, hist)
gs.save_aso()
if idx >= 0 and idx < len(hist):
cmd = hist[idx]
elif up:
if not found:
cmd = hist[-1]
else:
cmd = ''
view.replace(edit, r, '# %s \n' % cmd)
n = view.line(r.begin()).end()
view.sel().clear()
view.sel().add(sublime.Region(n, n))
class Gs9oInitCommand(sublime_plugin.TextCommand):
def run(self, edit, wd=None):
v = self.view
vs = v.settings()
if not wd:
wd = vs.get('9o.wd', active_wd(win=v.window()))
was_empty = v.size() == 0
s = '[ %s ] # \n' % gs.simple_fn(wd).replace('#', '~')
if was_empty:
v.insert(edit, 0, 'GoSublime %s 9o: type `help` for help and command documentation\n\n' % about.VERSION)
if was_empty or v.substr(v.size()-1) == '\n':
v.insert(edit, v.size(), s)
else:
v.insert(edit, v.size(), '\n'+s)
v.sel().clear()
n = v.size()-1
v.sel().add(sublime.Region(n, n))
opts = {
"rulers": [],
"fold_buttons": True,
"fade_fold_buttons": False,
"gutter": True,
"margin": 0,
# pad mostly so the completion menu shows on the first line
"line_padding_top": 1,
"line_padding_bottom": 1,
"tab_size": 2,
"word_wrap": True,
"indent_subsequent_lines": True,
"line_numbers": False,
"auto_complete": True,
"auto_complete_selector": "text",
"highlight_line": True,
"draw_indent_guides": True,
"scroll_past_end": True,
"indent_guide_options": ["draw_normal", "draw_active"],
"word_separators": "./\\()\"'-:,.;<>~!@#$%&*|+=[]{}`~?",
}
opts.update(gs.setting('9o_settings'))
for opt in opts:
vs.set(opt, opts[opt])
vs.set("9o", True)
vs.set("9o.wd", wd)
color_scheme = gs.setting("9o_color_scheme", "")
if color_scheme:
if color_scheme == "default":
vs.erase("color_scheme")
else:
vs.set("color_scheme", color_scheme)
else:
vs.set("color_scheme", "")
v.set_syntax_file(gs.tm_path('9o'))
if was_empty:
v.show(0)
else:
v.show(v.size()-1)
os.chdir(wd)
class Gs9oOpenCommand(sublime_plugin.TextCommand):
def run(self, edit, wd=None, run=[], save_hist=False, focus_view=True):
self.view.window().run_command('gs9o_win_open', {
'wd': wd,
'run': run,
'save_hist': save_hist,
'focus_view': focus_view,
})
class Gs9oWinOpenCommand(sublime_plugin.WindowCommand):
def run(self, wd=None, run=[], save_hist=False, focus_view=True):
win = self.window
wid = win.id()
if not wd:
wd = active_wd(win=win)
id = _wdid(wd)
st = stash.setdefault(wid, {})
v = st.get(id)
if v is None:
v = win.get_output_panel(id)
st[id] = v
win.run_command("show_panel", {"panel": ("output.%s" % id)})
if focus_view:
win.focus_view(v)
v.run_command('gs9o_init', {'wd': wd})
if run:
v.run_command('gs9o_paste_exec', {'cmd': ' '.join(run), 'save_hist': save_hist})
class Gs9oPasteExecCommand(sublime_plugin.TextCommand):
def run(self, edit, cmd, save_hist=False):
view = self.view
view.insert(edit, view.line(view.size()-1).end(), cmd)
view.sel().clear()
view.sel().add(view.line(view.size()-1).end())
view.run_command('gs9o_exec', {'save_hist': save_hist})
class Gs9oOpenSelectionCommand(sublime_plugin.TextCommand):
def is_enabled(self):
pos = gs.sel(self.view).begin()
return self.view.score_selector(pos, 'text.9o') > 0
def run(self, edit):
actions = []
v = self.view
sel = gs.sel(v)
if (sel.end() - sel.begin()) == 0:
pos = sel.begin()
inscope = lambda p: v.score_selector(p, 'path.9o') > 0
if inscope(pos):
actions.append(v.substr(v.extract_scope(pos)))
else:
pos -= 1
if inscope(pos):
actions.append(v.substr(v.extract_scope(pos)))
else:
line = v.line(pos)
for cr in v.find_by_selector('path.9o'):
if line.contains(cr):
actions.append(v.substr(cr))
else:
actions.append(v.substr(sel))
act_on(v, actions)
def act_on(view, actions):
for a in actions:
if act_on_path(view, a):
break
def act_on_path(view, path):
row = 0
col = 0
m = gs.VFN_ID_PAT.match(path)
if m:
path = 'gs.view://%s' % m.group(1)
m2 = gs.ROWCOL_PAT.match(m.group(2))
if m2:
row = int(m2.group(1))-1 if m2.group(1) else 0
col = int(m2.group(2))-1 if m2.group(2) else 0
else:
if URL_PATH_PAT.match(path):
if path.lower().startswith('gs.packages://'):
path = os.path.join(gs.packages_dir(), path[14:])
else:
try:
if not URL_SCHEME_PAT.match(path):
path = 'http://%s' % path
gs.notify(DOMAIN, 'open url: %s' % path)
webbrowser.open_new_tab(path)
return True
except Exception:
gs.error_traceback(DOMAIN)
return False
wd = view.settings().get('9o.wd') or active_wd()
m = SPLIT_FN_POS_PAT.match(path)
path = gs.apath((m.group(1) if m else path), wd)
row = max(0, int(m.group(2))-1 if (m and m.group(2)) else 0)
col = max(0, int(m.group(3))-1 if (m and m.group(3)) else 0)
if m or os.path.exists(path):
gs.focus(path, row, col, win=view.window())
return True
else:
gs.notify(DOMAIN, "Invalid path `%s'" % path)
return False
def _exparg(s, m):
s = string.Template(s).safe_substitute(m)
s = os.path.expanduser(s)
return s
class Gs9oExecCommand(sublime_plugin.TextCommand):
def is_enabled(self):
pos = gs.sel(self.view).begin()
return self.view.score_selector(pos, 'text.9o') > 0
def run(self, edit, save_hist=False):
view = self.view
pos = gs.sel(view).begin()
line = view.line(pos)
wd = view.settings().get('9o.wd')
try:
os.chdir(wd)
except Exception:
gs.error_traceback(DOMAIN)
ln = view.substr(line).split('#', 1)
if len(ln) == 2:
cmd = ln[1].strip()
if cmd:
vs = view.settings()
aso = gs.aso()
hkey = _hkey(wd)
hist = gs.dval(aso.get(hkey), [])
m = HIST_EXPAND_PAT.match(cmd)
if m:
pfx = m.group(1)
hl = len(hist)
idx = hl - int(m.group(2))
cmd = ''
if idx >= 0 and idx < hl:
cmd = hist[idx]
if pfx == '^' or not cmd:
view.replace(edit, line, ('%s# %s' % (ln[0], cmd)))
return
elif save_hist:
try:
hist.remove(cmd)
except ValueError:
pass
hist.append(cmd)
aso.set(hkey, hist)
gs.save_aso()
if not cmd:
view.run_command('gs9o_init')
return
view.replace(edit, line, (u'[ `%s` %s ]' % (cmd, HOURGLASS)))
rkey = '9o.exec.%s' % uuid.uuid4()
view.add_regions(rkey, [sublime.Region(line.begin(), view.size())], '')
view.run_command('gs9o_init')
nv = sh.env()
anv = nv.copy()
seen = {}
am = aliases()
while True:
cli = cmd.split(' ', 1)
nm = cli[0]
if not nm:
break
ag = cli[1].strip() if len(cli) == 2 else ''
alias = am.get(nm, '')
if not alias:
break
if alias in seen:
gs.error(DOMAIN, 'recursive alias detected: `%s`' % alias)
break
seen[alias] = True
anv['_args'] = ag
cmd = string.Template(alias).safe_substitute(anv)
if nm != 'sh':
f = builtins().get(nm)
if f:
args = []
if ag:
args = [_exparg(s, nv) for s in shlex.split(gs.astr(ag))]
f(view, edit, args, wd, rkey)
return
if nm == 'sh':
args = sh.cmd(ag)
else:
args = sh.cmd(cmd)
cmd_sh(view, edit, args, wd, rkey)
else:
view.insert(edit, gs.sel(view).begin(), '\n')
class Gs9oPushOutput(sublime_plugin.TextCommand):
def run(self, edit, rkey, output, hourglass_repl=''):
view = self.view
output = '\t%s' % gs.ustr(output).strip().replace('\r', '').replace('\n', '\n\t')
regions = view.get_regions(rkey)
if regions:
line = view.line(regions[0].begin())
lsrc = view.substr(line).replace(HOURGLASS, (hourglass_repl or '| done'))
view.replace(edit, line, lsrc)
r = line
if output.strip():
line = view.line(regions[0].begin())
view.insert(edit, line.end(), '\n%s' % output)
r = view.get_regions(rkey)[0]
else:
n = view.size()
view.insert(edit, n, '\n%s' % output)
r = sublime.Region(n, view.size())
if gs.setting('9o_show_end') is True:
view.show(r.end())
else:
view.show(r.begin())
class Gs9oRunManyCommand(sublime_plugin.TextCommand):
def run(self, edit, wd=None, commands=[], save_hist=False, focus_view=False):
for run in commands:
self.view.run_command("gs9o_open", {
'run': run,
'wd': wd,
'save_hist': save_hist,
'focus_view': focus_view,
})
def aliases():
return gs.setting('9o_aliases', {}).copy()
def builtins():
m = gs.gs9o.copy()
g = globals()
for k, v in g.items():
if k.startswith('cmd_'):
k = k[4:].replace('_', '-')
if k and k not in m:
m[k] = v
return m
def push_output(view, rkey, output, hourglass_repl=''):
def f():
view.run_command('gs9o_push_output', {
'rkey': rkey,
'output': output,
'hourglass_repl': hourglass_repl,
})
sublime.set_timeout(f, 0)
def _save_all(win, wd):
if gs.setting('autosave') is True and win is not None:
for v in win.views():
try:
fn = v.file_name()
if fn and v.is_dirty() and fn.endswith('.go') and os.path.dirname(fn) == wd:
v.run_command('gs_fmt_save')
except Exception:
gs.error_traceback(DOMAIN)
def _9_begin_call(name, view, edit, args, wd, rkey, cid):
dmn = '%s: 9 %s' % (DOMAIN, name)
msg = '[ %s ] # 9 %s' % (gs.simple_fn(wd), ' '.join(args))
if not cid:
cid = '9%s-%s' % (name, uuid.uuid4())
tid = gs.begin(dmn, msg, set_status=False, cancel=lambda: mg9.acall('kill', {'cid': cid}, None))
tid_alias['%s-%s' % (name, wd)] = tid
def cb(res, err):
out = '\n'.join(s for s in (res.get('out'), res.get('err'), err) if s)
tmp_fn = res.get('tmpFn')
fn = res.get('fn')
if fn and tmp_fn:
bfn = os.path.basename(tmp_fn)
repls = [
'./%s' % bfn,
'.\\%s' % bfn,
tmp_fn,
]
for s in repls:
out = out.replace(s, fn)
def f():
gs.end(tid)
push_output(view, rkey, out, hourglass_repl='| done: %s' % res.get('dur', ''))
sublime.set_timeout(f, 0)
return cid, cb
def cmd_echo(view, edit, args, wd, rkey):
push_output(view, rkey, ' '.join(args))
def cmd_which(view, edit, args, wd, rkey):
l = []
am = aliases()
m = builtins()
if not args:
args = []
args.extend(sorted(m.keys()))
args.extend(sorted(am.keys()))
fm = '%{0}s: %s'.format(max(len(s) for s in args))
for k in args:
if k == 'sh':
v = '9o builtin: %s' % sh.cmd('${CMD}')
elif k in ('go'):
v = '9o builtin: %s' % sh.which(k)
elif k in m:
v = '9o builtin'
elif k in am:
v = '9o alias: `%s`' % am[k]
else:
v = sh.which(k)
l.append(fm % (k, v))
push_output(view, rkey, '\n'.join(l))
def cmd_cd(view, edit, args, wd, rkey):
try:
if args:
wd = args[0]
wd = string.Template(wd).safe_substitute(sh.env())
wd = os.path.expanduser(wd)
wd = os.path.abspath(wd)
else:
fn = view.window().active_view().file_name()
if fn:
wd = os.path.dirname(fn)
os.chdir(wd)
except Exception as ex:
push_output(view, rkey, 'Cannot chdir: %s' % ex)
return
push_output(view, rkey, '')
view.run_command('gs9o_init', {'wd': wd})
def cmd_reset(view, edit, args, wd, rkey):
push_output(view, rkey, '')
view.erase(edit, sublime.Region(0, view.size()))
view.run_command('gs9o_init')
def cmd_clear(view, edit, args, wd, rkey):
cmd_reset(view, edit, args, wd, rkey)
def cmd_go(view, edit, args, wd, rkey):
_save_all(view.window(), wd)
cid, cb = _9_begin_call('go', view, edit, args, wd, rkey, '9go-%s' % wd)
a = {
'cid': cid,
'env': sh.env(),
'cwd': wd,
'cmd': {
'name': 'go',
'args': args,
}
}
sublime.set_timeout(lambda: mg9.acall('sh', a, cb), 0)
def cmd_cancel_replay(view, edit, args, wd, rkey):
cid = ''
av = None
win = view.window()
if win is not None:
av = win.active_view()
if av is not None and not av.file_name():
cid = '9replayv-%s' % av.id()
if not cid:
cid = '9replay-%s' % wd
mg9.acall('kill', {'cid': cid}, None)
push_output(view, rkey, '')
def cmd_sh(view, edit, args, wd, rkey):
cid, cb = _9_begin_call('sh', view, edit, args, wd, rkey, '')
a = {
'cid': cid,
'env': sh.env(),
'cwd': wd,
'cmd': {
'name': args[0],
'args': args[1:],
}
}
sublime.set_timeout(lambda: mg9.acall('sh', a, cb), 0)
def cmd_share(view, edit, args, wd, rkey):
av = gs.active_valid_go_view(win=view.window())
if av is None:
push_output(view, rkey, 'not sharing non-go src')
return
def f(res, err):
s = '%s\n%s' % (err, res.get('url', ''))
push_output(view, rkey, s.strip())
mg9.share(gs.view_src(view.window().active_view()), f)
def cmd_help(view, edit, args, wd, rkey):
gs.focus(gs.dist_path('9o.md'))
push_output(view, rkey, '')
def cmd_run(view, edit, args, wd, rkey):
cmd_9(view, edit, gs.lst('run', args), wd, rkey)
def cmd_replay(view, edit, args, wd, rkey):
cmd_9(view, edit, gs.lst('replay', args), wd, rkey)
def cmd_build(view, edit, args, wd, rkey):
cmd_9(view, edit, gs.lst('build', args), wd, rkey)
def cmd_9(view, edit, args, wd, rkey):
if len(args) == 0 or args[0] not in ('run', 'replay', 'build'):
push_output(view, rkey, ('9: invalid args %s' % args))
return
subcmd = args[0]
cid = ''
if subcmd == 'replay':
cid = '9replay-%s' % wd
cid, cb = _9_begin_call(subcmd, view, edit, args, wd, rkey, cid)
a = {
'cid': cid,
'env': sh.env(),
'dir': wd,
'args': args[1:],
'build_only': (subcmd == 'build'),
}
win = view.window()
if win is not None:
av = win.active_view()
if av is not None:
fn = av.file_name()
if fn:
_save_all(win, wd)
else:
if gs.is_go_source_view(av, False):
a['fn'] = gs.view_fn(av)
a['src'] = av.substr(sublime.Region(0, av.size()))
sublime.set_timeout(lambda: mg9.acall('play', a, cb), 0)
def cmd_tskill(view, edit, args, wd, rkey):
if len(args) == 0:
sublime.set_timeout(lambda: sublime.active_window().run_command("gs_show_tasks"), 0)
push_output(view, rkey, '')
return
l = []
for tid in args:
tid = tid.lstrip('#')
tid = tid_alias.get('%s-%s' % (tid, wd), tid)
l.append('kill %s: %s' % (tid, ('yes' if gs.cancel_task(tid) else 'no')))
push_output(view, rkey, '\n'.join(l))
def _env_settings(d, view, edit, args, wd, rkey):
if len(args) > 0:
m = {}
for k in args:
m[k] = d.get(k)
else:
m = d
s = '\n'.join((
'Default Settings file: gs.packages://GoSublime/GoSublime.sublime-settings (do not edit this file)',
'User settings file: gs.packages://User/GoSublime.sublime-settings (add/change your settings here)',
json.dumps(m, sort_keys=True, indent=4),
))
push_output(view, rkey, s)
def cmd_settings(view, edit, args, wd, rkey):
_env_settings(gs.settings_dict(), view, edit, args, wd, rkey)
def cmd_env(view, edit, args, wd, rkey):
_env_settings(sh.env(), view, edit, args, wd, rkey)
def cmd_hist(view, edit, args, wd, rkey):
aso = gs.aso()
hkey = _hkey(wd)
s = 'hist: invalid args: %s' % args
if len(args) == 0:
hist = gs.dval(aso.get(hkey), [])
hist.reverse()
hlen = len(hist)
s = '\n'.join('^%d: %s' % (i+1, v) for i,v in enumerate(hist))
elif len(args) == 1:
if args[0] == 'erase':
aso.erase(hkey)
gs.save_aso()
s = ''
push_output(view, rkey, s)