-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmouse.go
More file actions
159 lines (137 loc) · 3.5 KB
/
mouse.go
File metadata and controls
159 lines (137 loc) · 3.5 KB
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
package main
import (
"os/exec"
termbox "github.com/nsf/termbox-go"
)
const (
GomacsMouseNone byte = iota
GomacsMouseDragging
)
func (t *winTree) mouseInBuffer(x, y, wx, wy, mx, my int) (int, int, *EditorBuffer) {
if !(x <= mx && mx <= x+wx && y <= my && my <= y+wx) {
// Do nothing
return 0, 0, nil
} else if t.split {
if t.hor {
rx, ry, rbuf := t.childLT.mouseInBuffer(x, y, wx/2, wy, mx, my)
if rbuf != nil {
return rx, ry, rbuf
}
return t.childRB.mouseInBuffer(x+(wx/2)+1, y, (wx / 2),
wy, mx, my)
}
rx, ry, rbuf := t.childLT.mouseInBuffer(x, y, wx, wy/2, mx, my)
if rbuf != nil {
return rx, ry, rbuf
}
return t.childRB.mouseInBuffer(x, y+1+(wy/2), wx, (wy/2)-1, mx, my)
}
cy := t.buf.rowoff + my%wy
if cy >= Global.CurrentB.NumRows {
return 0, cy, t.buf
}
row := Global.CurrentB.Rows[cy]
gut := 0
if Global.CurrentB.hasMode("line-number-mode") {
gut = GetGutterWidth(Global.CurrentB.NumRows)
}
rx := mx - x - gut + row.coloff
Global.WindowTree.mapTree(func(wt *winTree) { wt.focused = false })
t.setFocus()
return editorRowRxToCx(row, rx), cy, t.buf
}
func getMousePoint(mx, my int) (int, int, *EditorBuffer) {
sx, sy := termbox.Size()
return Global.WindowTree.mouseInBuffer(0, 0, sx, sy-2, mx, my)
}
func JumpToMousePoint() {
if Global.CurrentB.NumRows <= 0 {
return
}
var cx, cy int
cx, cy, Global.CurrentB = getMousePoint(Global.MouseX, Global.MouseY)
if cy >= Global.CurrentB.NumRows {
Global.CurrentB.cy = Global.CurrentB.NumRows - 1
Global.CurrentB.cx = Global.CurrentB.Rows[Global.CurrentB.cy].Size
} else {
Global.CurrentB.cy = cy
Global.CurrentB.cx = cx
Global.CurrentB.prefcx = cx
}
}
var mousestate byte = GomacsMouseNone
func MouseDragRegion() {
buf := Global.CurrentB
if buf.NumRows <= 0 {
return
}
cachedcx, cachedcy := buf.cx, buf.cy
JumpToMousePoint()
if mousestate == GomacsMouseDragging && buf == Global.CurrentB && (cachedcx != buf.cx || cachedcy != buf.cy) {
if !buf.regionActive {
buf.MarkX = cachedcx
buf.MarkY = cachedcy
buf.regionActive = true
}
buf.recalcRegion()
}
mousestate = GomacsMouseDragging
}
func MouseScrollUp() {
_, _, Global.CurrentB = getMousePoint(Global.MouseX, Global.MouseY)
if Global.CurrentB.rowoff > 0 {
Global.CurrentB.rowoff--
} else {
Global.Input = "Beginning of buffer"
}
if Global.CurrentB.cy >= Global.CurrentB.rowoff+Global.CurrentBHeight-1 {
Global.CurrentB.MoveCursorUp()
}
}
func MouseScrollDown() {
_, _, Global.CurrentB = getMousePoint(Global.MouseX, Global.MouseY)
if Global.CurrentB.rowoff < Global.CurrentB.NumRows {
Global.CurrentB.rowoff++
} else {
Global.Input = "End of buffer"
}
if Global.CurrentB.cy < Global.CurrentB.rowoff {
Global.CurrentB.MoveCursorDown()
}
}
func MouseRelease() {
Global.Input = ""
mousestate = GomacsMouseNone
}
func MouseYankXsel() {
prog, args, err := getXsel()
if err != nil {
Global.Input = "Can't find xsel or xclip in your PATH"
AddErrorMessage(Global.Input)
return
}
var out string
out, err = shellCmd(prog, args)
if err != nil {
Global.Input = err.Error()
AddErrorMessage(Global.Input)
return
}
Global.Clipboard = out
if Global.CurrentB.hasMode("xsel-jump-to-cursor-mode") {
JumpToMousePoint()
}
doYankRegion()
Global.Input = "Yanked X selection."
}
func getXsel() (string, []string, error) {
args := []string{}
ret, err := exec.LookPath("xsel")
if err != nil {
ret, err = exec.LookPath("xclip")
if err == nil {
args = []string{"-o", "-selection", "primary"}
}
}
return ret, args, err
}