-
Notifications
You must be signed in to change notification settings - Fork 3
/
history.go
89 lines (78 loc) · 2.18 KB
/
history.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
package readline
import (
"context"
)
// IHistory is the interface ReadLine can use as container for history.
// It can be set to Editor.History field
type IHistory interface {
Len() int
At(int) string
}
type _EmptyHistory struct{}
// Len always returns zero because the receiver is dummy.
func (_EmptyHistory) Len() int { return 0 }
// At always returns empty-string because the receiver is dummy.
func (_EmptyHistory) At(int) string { return "" }
// CmdPreviousHistory is the command that replaces the line to the previous entry in the history (Ctrl-P)
var CmdPreviousHistory = NewGoCommand("PREVIOUS_HISTORY", cmdPreviousHistory)
func (B *Buffer) saveModifiedHistory() {
s := B.String()
if B.historyPointer < B.History.Len() && B.History.At(B.historyPointer) == s {
return
}
if B.modifiedHistory == nil {
B.modifiedHistory = make(map[int]string)
}
B.modifiedHistory[B.historyPointer] = s
}
func (B *Buffer) getHistory() string {
s, ok := B.modifiedHistory[B.historyPointer]
if ok {
return s
}
if B.historyPointer < 0 || B.historyPointer >= B.History.Len() {
return ""
}
return B.History.At(B.historyPointer)
}
func cmdPreviousHistory(ctx context.Context, this *Buffer) Result {
if this.History.Len() <= 0 {
return CONTINUE
}
if this.historyPointer <= 0 {
if !this.HistoryCycling {
return CONTINUE
}
this.historyPointer = this.History.Len() + 1
}
this.saveModifiedHistory()
this.historyPointer--
CmdKillWholeLine.Func(ctx, this)
if s := this.getHistory(); s != "" {
this.InsertString(0, s)
this.ViewStart = 0
this.Cursor = 0
CmdEndOfLine.Func(ctx, this)
}
return CONTINUE
}
// CmdNextHistory is the command that replaces the line to the next entry of the history. (Ctrl-N)
var CmdNextHistory = NewGoCommand("NEXT_HISTORY", cmdNextHistory)
func cmdNextHistory(ctx context.Context, this *Buffer) Result {
if this.History.Len() <= 0 {
return CONTINUE
}
if this.historyPointer+1 > this.History.Len() {
return CONTINUE
}
this.saveModifiedHistory()
this.historyPointer++
CmdKillWholeLine.Func(ctx, this)
if s := this.getHistory(); s != "" {
this.InsertString(0, s)
this.ViewStart = 0
this.Cursor = 0
CmdEndOfLine.Func(ctx, this)
}
return CONTINUE
}