-
Notifications
You must be signed in to change notification settings - Fork 87
/
window.go
168 lines (141 loc) · 3.35 KB
/
window.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
// Copyright 2013 The lime Authors.
// Use of this source code is governed by a 2-clause
// BSD-style license that can be found in the LICENSE file.
package backend
import (
"fmt"
"io/ioutil"
"path/filepath"
"runtime/debug"
"sync"
"github.com/limetext/backend/log"
"github.com/limetext/text"
)
type Window struct {
text.HasSettings
text.HasId
views []*View
active_view *View
project *Project
lock sync.Mutex
}
// implement the fmt.Stringer interface
func (w *Window) String() string {
return fmt.Sprintf("Window{id:%d}", w.Id())
}
func (w *Window) NewFile() *View {
w.lock.Lock()
defer w.lock.Unlock()
w.views = append(w.views, newView(w))
v := w.views[len(w.views)-1]
v.setBuffer(text.NewBuffer())
v.Sel().Clear()
v.Sel().Add(text.Region{A: 0, B: 0})
v.Settings().Set("lime.last_save_change_count", v.ChangeCount())
OnNew.Call(v)
w.SetActiveView(v)
return v
}
func (w *Window) Views() []*View {
w.lock.Lock()
defer w.lock.Unlock()
ret := make([]*View, len(w.views))
copy(ret, w.views)
return ret
}
func (w *Window) remove(v *View) {
w.lock.Lock()
defer w.lock.Unlock()
for i, vv := range w.views {
if v == vv {
end := len(w.views) - 1
if i != end {
copy(w.views[i:], w.views[i+1:])
}
w.views = w.views[:end]
return
}
}
log.Error("Wanted to remove view %s, but it doesn't appear to be a child of this window", v)
}
func (w *Window) OpenFile(filename string, flags int) *View {
v := w.NewFile()
v.SetScratch(true)
e := v.BeginEdit()
if fn, err := filepath.Abs(filename); err != nil {
v.SetFileName(filename)
} else {
v.SetFileName(fn)
}
if d, err := ioutil.ReadFile(filename); err != nil {
log.Error("Couldn't load file %s: %s", filename, err)
} else {
v.Insert(e, 0, string(d))
}
v.EndEdit(e)
v.Sel().Clear()
v.Sel().Add(text.Region{A: 0, B: 0})
v.Settings().Set("lime.last_save_change_count", v.ChangeCount())
v.SetScratch(false)
OnLoad.Call(v)
return v
}
func (w *Window) SetActiveView(v *View) {
if w.active_view != nil {
OnDeactivated.Call(w.active_view)
}
w.active_view = v
if w.active_view != nil {
OnActivated.Call(w.active_view)
}
}
func (w *Window) ActiveView() *View {
return w.active_view
}
// Closes the Window and all its Views.
// Returns "true" if the Window closed successfully. Otherwise returns "false".
func (w *Window) Close() bool {
if !w.CloseAllViews() {
return false
}
GetEditor().remove(w)
return true
}
// Closes all of the Window's Views.
// Returns "true" if all the Views closed successfully. Otherwise returns "false".
func (w *Window) CloseAllViews() bool {
for len(w.views) > 0 {
if !w.views[0].Close() {
return false
}
}
return true
}
func (w *Window) runCommand(c WindowCommand, name string) error {
defer func() {
if r := recover(); r != nil {
log.Error("Paniced while running window command %s %v: %v\n%s", name, c, r, string(debug.Stack()))
}
}()
return c.Run(w)
}
func (w *Window) OpenProject(name string) *Project {
if err := w.Project().Load(name); err != nil {
log.Error(err)
return nil
}
if abs, err := filepath.Abs(name); err != nil {
w.Project().SetName(name)
} else {
w.Project().SetName(abs)
}
GetEditor().Watch(w.Project().FileName(), w.Project())
OnProjectChanged.Call(w)
return w.Project()
}
func (w *Window) Project() *Project {
if w.project == nil {
w.project = newProject(w)
}
return w.project
}