forked from elves/elvish
/
editor.go
482 lines (409 loc) · 11.6 KB
/
editor.go
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
// Package edit implements a command line editor.
package edit
import (
"bytes"
"fmt"
"os"
"sync"
"syscall"
"time"
"github.com/elves/elvish/daemon/api"
"github.com/elves/elvish/edit/highlight"
"github.com/elves/elvish/edit/history"
"github.com/elves/elvish/edit/tty"
"github.com/elves/elvish/edit/ui"
"github.com/elves/elvish/eval"
"github.com/elves/elvish/parse"
"github.com/elves/elvish/sys"
"github.com/elves/elvish/util"
)
var logger = util.GetLogger("[edit] ")
const (
lackEOLRune = '\u23ce'
lackEOL = "\033[7m" + string(lackEOLRune) + "\033[m"
)
// Editor keeps the status of the line editor.
type Editor struct {
in *os.File
out *os.File
writer *Writer
reader *tty.Reader
sigs chan os.Signal
daemon *api.Client
evaler *eval.Evaler
variables map[string]eval.Variable
active bool
activeMutex sync.Mutex
historyFuser *history.Fuser
historyMutex sync.RWMutex
editorState
}
type editorState struct {
// States used during ReadLine. Reset at the beginning of ReadLine.
savedTermios *sys.Termios
notificationMutex sync.Mutex
notifications []string
tips []string
line string
lexedLine *string
chunk *parse.Chunk
styling *highlight.Styling
promptContent []*ui.Styled
rpromptContent []*ui.Styled
dot int
mode Mode
insert insert
command command
completion completion
navigation navigation
hist hist
// A cache of external commands, used in stylist.
isExternal map[string]bool
parseErrorAtEnd bool
// Used for builtins.
lastKey ui.Key
nextAction action
}
// NewEditor creates an Editor.
func NewEditor(in *os.File, out *os.File, sigs chan os.Signal, ev *eval.Evaler, daemon *api.Client) *Editor {
ed := &Editor{
in: in,
out: out,
writer: newWriter(out),
reader: tty.NewReader(in),
sigs: sigs,
daemon: daemon,
evaler: ev,
variables: makeVariables(),
}
if daemon != nil {
f, err := history.NewFuser(daemon)
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to initialize command history. Disabled.")
} else {
ed.historyFuser = f
}
}
ev.Editor = ed
installModules(ev.Modules, ed)
return ed
}
// Active returns the activeness of the Editor.
func (ed *Editor) Active() bool {
return ed.active
}
// ActiveMutex returns a mutex that must be used when changing the activeness of
// the Editor.
func (ed *Editor) ActiveMutex() *sync.Mutex {
return &ed.activeMutex
}
func (ed *Editor) flash() {
// TODO implement fish-like flash effect
}
func (ed *Editor) addTip(format string, args ...interface{}) {
ed.tips = append(ed.tips, fmt.Sprintf(format, args...))
}
// Notify adds one notification entry. It is concurrency-safe.
func (ed *Editor) Notify(format string, args ...interface{}) {
ed.notificationMutex.Lock()
defer ed.notificationMutex.Unlock()
ed.notifications = append(ed.notifications, fmt.Sprintf(format, args...))
}
func (ed *Editor) refresh(fullRefresh bool, addErrorsToTips bool) error {
src := ed.line
// Re-lex the line if needed
if ed.lexedLine == nil || *ed.lexedLine != src {
ed.lexedLine = &src
n, err := parse.Parse("[interactive]", src)
ed.chunk = n
ed.parseErrorAtEnd = err != nil && atEnd(err, len(src))
// If all parse errors are at the end, it is likely caused by incomplete
// input. In that case, do not complain about parse errors.
// TODO(xiaq): Find a more reliable way to determine incomplete input.
// Ideally the parser should report it.
if err != nil && addErrorsToTips && !ed.parseErrorAtEnd {
ed.addTip("%s", err)
}
ed.styling = &highlight.Styling{}
doHighlight(n, ed)
_, err = ed.evaler.Compile(n, "[interactive]", src)
if err != nil && !atEnd(err, len(src)) {
if addErrorsToTips {
ed.addTip("%s", err)
}
// Highlight errors in the input buffer.
// TODO(xiaq): There might be multiple tokens involved in the
// compiler error; they should all be highlighted as erroneous.
p := err.(*eval.CompilationError).Context.Begin
badn := findLeafNode(n, p)
ed.styling.Add(badn.Begin(), badn.End(), styleForCompilerError.String())
}
}
return ed.writer.refresh(&ed.editorState, fullRefresh)
}
func atEnd(e error, n int) bool {
switch e := e.(type) {
case *eval.CompilationError:
return e.Context.Begin == n
case *parse.Error:
for _, entry := range e.Entries {
if entry.Context.Begin != n {
return false
}
}
return true
default:
logger.Printf("atEnd called with error type %T", e)
return false
}
}
// insertAtDot inserts text at the dot and moves the dot after it.
func (ed *Editor) insertAtDot(text string) {
ed.line = ed.line[:ed.dot] + text + ed.line[ed.dot:]
ed.dot += len(text)
}
const flushInputDuringSetup = false
func setupTerminal(file *os.File) (*sys.Termios, error) {
fd := int(file.Fd())
term, err := sys.NewTermiosFromFd(fd)
if err != nil {
return nil, fmt.Errorf("can't get terminal attribute: %s", err)
}
savedTermios := term.Copy()
term.SetICanon(false)
term.SetEcho(false)
term.SetVMin(1)
term.SetVTime(0)
err = term.ApplyToFd(fd)
if err != nil {
return nil, fmt.Errorf("can't set up terminal attribute: %s", err)
}
if flushInputDuringSetup {
err = sys.FlushInput(fd)
if err != nil {
return nil, fmt.Errorf("can't flush input: %s", err)
}
}
return savedTermios, nil
}
// startReadLine prepares the terminal for the editor.
func (ed *Editor) startReadLine() error {
ed.activeMutex.Lock()
defer ed.activeMutex.Unlock()
ed.active = true
savedTermios, err := setupTerminal(ed.in)
if err != nil {
return err
}
ed.savedTermios = savedTermios
_, width := sys.GetWinsize(int(ed.in.Fd()))
/*
Write a lackEOLRune if the cursor is not in the leftmost column. This is
done as follows:
1. Turn on autowrap;
2. Write lackEOL along with enough padding, so that the total width is
equal to the width of the screen.
If the cursor was in the first column, we are still in the same line,
just off the line boundary. Otherwise, we are now in the next line.
3. Rewind to the first column, write one space and rewind again. If the
cursor was in the first column to start with, we have just erased the
LackEOL character. Otherwise, we are now in the next line and this is
a no-op. The LackEOL character remains.
*/
fmt.Fprintf(ed.out, "\033[?7h%s%*s\r \r", lackEOL, width-util.Wcwidth(lackEOLRune), "")
/*
Turn off autowrap.
The terminals sometimes has different opinions about how wide some
characters are (notably emojis and some dingbats) with elvish. When that
happens, elvish becomes wrong about where the cursor is when it writes
its output, and the effect can be disastrous.
If we turn off autowrap, the terminal won't insert any newlines behind
the scene, so elvish is always right about which line the cursor is.
With a bit more caution, this can restrict the consequence of the
mismatch within one line.
*/
ed.out.WriteString("\033[?7l")
// Turn on SGR-style mouse tracking.
//ed.out.WriteString("\033[?1000;1006h")
// Enable bracketed paste.
ed.out.WriteString("\033[?2004h")
return nil
}
// finishReadLine puts the terminal in a state suitable for other programs to
// use.
func (ed *Editor) finishReadLine(addError func(error)) {
ed.activeMutex.Lock()
defer ed.activeMutex.Unlock()
ed.active = false
// Refresh the terminal for the last time in a clean-ish state.
ed.mode = &ed.insert
ed.tips = nil
ed.dot = len(ed.line)
if !ed.rpromptPersistent() {
ed.rpromptContent = nil
}
addError(ed.refresh(false, false))
ed.out.WriteString("\n")
ed.writer.resetOldBuf()
ed.reader.Quit()
// Turn on autowrap.
ed.out.WriteString("\033[?7h")
// Turn off mouse tracking.
//ed.out.WriteString("\033[?1000;1006l")
// Disable bracketed paste.
ed.out.WriteString("\033[?2004l")
// Restore termios.
err := ed.savedTermios.ApplyToFd(int(ed.in.Fd()))
if err != nil {
addError(fmt.Errorf("can't restore terminal attribute: %s", err))
}
// Save the line before resetting all of editorState.
line := ed.line
ed.editorState = editorState{}
callHooks(ed.evaler, ed.afterReadLine(), eval.String(line))
}
// ReadLine reads a line interactively.
func (ed *Editor) ReadLine() (line string, err error) {
e := ed.startReadLine()
if e != nil {
return "", e
}
defer ed.finishReadLine(func(e error) {
if e != nil {
err = util.CatError(err, e)
}
})
ed.mode = &ed.insert
// Find external commands asynchronously, so that slow I/O won't block the
// editor.
isExternalCh := make(chan map[string]bool, 1)
go getIsExternal(ed.evaler, isExternalCh)
go ed.reader.Run()
fullRefresh := false
callHooks(ed.evaler, ed.beforeReadLine())
MainLoop:
for {
ed.promptContent = callPrompt(ed, ed.prompt())
ed.rpromptContent = callPrompt(ed, ed.rprompt())
err := ed.refresh(fullRefresh, true)
fullRefresh = false
if err != nil {
return "", err
}
ed.tips = nil
select {
case m := <-isExternalCh:
ed.isExternal = m
case sig := <-ed.sigs:
// TODO(xiaq): Maybe support customizable handling of signals
switch sig {
case syscall.SIGINT:
// Start over
ed.editorState = editorState{
savedTermios: ed.savedTermios,
isExternal: ed.isExternal,
}
ed.mode = &ed.insert
continue MainLoop
case syscall.SIGWINCH:
fullRefresh = true
continue MainLoop
case syscall.SIGCHLD:
// ignore
default:
ed.addTip("ignored signal %s", sig)
}
case err := <-ed.reader.ErrorChan():
ed.Notify("reader error: %s", err.Error())
case unit := <-ed.reader.UnitChan():
switch unit := unit.(type) {
case tty.MouseEvent:
ed.addTip("mouse: %+v", unit)
case tty.CursorPosition:
// Ignore CPR
case tty.PasteSetting:
if !unit {
continue
}
var buf bytes.Buffer
timer := time.NewTimer(tty.EscSequenceTimeout)
paste:
for {
// XXX Should also select on other chans. However those chans
// will be unified (again) into one later so we don't do
// busywork here.
select {
case unit := <-ed.reader.UnitChan():
switch unit := unit.(type) {
case tty.Key:
k := ui.Key(unit)
if k.Mod != 0 {
ed.Notify("function key within paste, aborting")
break paste
}
buf.WriteRune(k.Rune)
timer.Reset(tty.EscSequenceTimeout)
case tty.PasteSetting:
if !unit {
break paste
}
default: // Ignore other things.
}
case <-timer.C:
ed.Notify("bracketed paste timeout")
break paste
}
}
topaste := buf.String()
if ed.insert.quotePaste {
topaste = parse.Quote(topaste)
}
ed.insertAtDot(topaste)
case tty.RawRune:
insertRaw(ed, rune(unit))
case tty.Key:
k := ui.Key(unit)
lookupKey:
fn := ed.mode.Binding(k)
if fn == nil {
ed.addTip("Unbound and no default binding: %s", k)
continue MainLoop
}
ed.insert.insertedLiteral = false
ed.lastKey = k
ed.CallFn(fn)
if ed.insert.insertedLiteral {
ed.insert.literalInserts++
} else {
ed.insert.literalInserts = 0
}
act := ed.nextAction
ed.nextAction = action{}
switch act.typ {
case noAction:
continue
case reprocessKey:
err := ed.refresh(false, true)
if err != nil {
return "", err
}
goto lookupKey
case exitReadLine:
if act.returnErr == nil && act.returnLine != "" {
ed.appendHistory(act.returnLine)
}
return act.returnLine, act.returnErr
}
}
}
}
}
// getIsExternal finds a set of all external commands and puts it on the result
// channel.
func getIsExternal(ev *eval.Evaler, result chan<- map[string]bool) {
isExternal := make(map[string]bool)
ev.EachExternal(func(name string) {
isExternal[name] = true
})
result <- isExternal
}