-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfind.go
115 lines (108 loc) · 2.69 KB
/
find.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
package main
import (
"github.com/mattn/go-gtk/gtk"
"github.com/mattn/go-gtk/gdk"
"strings"
)
func find_global(pattern string, find_file bool) {
var pos int
if find_file {
prev_pattern = ""
} else {
prev_pattern = pattern
}
search_view.store.Clear()
for name, rec := range file_map {
if find_file {
pos = strings.Index(name, pattern)
} else {
if name == cur_file {
// find_in_current_file does required work for cur_file.
continue
}
pos = strings.Index(string(rec.buf), pattern)
}
if -1 != pos {
search_view.AddFile(name)
}
}
}
func find_cb() {
find_common(false)
}
func find_file_cb() {
find_common(true)
}
func find_common(find_file bool) {
found_in_cur_file := false
dialog_ok, pattern, global, find_file := find_dialog(find_file)
if false == dialog_ok {
return
}
if global {
search_view.PrepareToSearch()
}
if find_file {
find_global(pattern, true)
} else {
if global {
find_global(pattern, false)
}
found_in_cur_file = find_in_current_file(pattern, global)
}
if global && !found_in_cur_file {
search_view.SetCursor(0)
}
}
// Returns true if pattern was found in current file, false o/w.
func find_in_current_file(pattern string, global bool) bool {
var be, en gtk.TextIter
source_buf.GetSelectionBounds(&be, &en)
if find_next_instance(&en, &be, &en, pattern) {
move_focus_and_selection(&be, &en)
mark_set_cb()
if global {
search_view.AddFile(cur_file)
}
return true
}
return false
}
func find_dialog(find_file bool) (bool, string, bool, bool) {
dialog := gtk.NewDialog()
defer dialog.Destroy()
dialog.SetTitle("Find")
dialog.AddButton("_Find", gtk.RESPONSE_ACCEPT)
dialog.AddButton("_Cancel", gtk.RESPONSE_CANCEL)
w := dialog.GetWidgetForResponse(int(gtk.RESPONSE_ACCEPT))
dialog.AddAccelGroup(accel_group)
w.AddAccelerator("clicked", accel_group, gdk.KEY_Return,
0, gtk.ACCEL_VISIBLE)
entry := find_entry_with_history()
global_button := gtk.NewCheckButtonWithLabel("Global")
global_button.SetVisible(true)
global_button.SetActive(prev_global)
file_button := gtk.NewCheckButtonWithLabel("Find file by name pattern")
file_button.SetVisible(true)
file_button.SetActive(find_file)
vbox := dialog.GetVBox()
vbox.Add(entry)
vbox.Add(global_button)
vbox.Add(file_button)
if gtk.RESPONSE_ACCEPT == dialog.Run() {
entry_text := entry.GetActiveText()
if nil == search_history {
search_history = make([]string, 1)
search_history[0] = entry_text
} else {
be := 0
if 10 <= len(search_history) {
be = 1
}
search_history = append(search_history[be:], entry_text)
}
prev_global = global_button.GetActive()
return true, entry_text, prev_global, file_button.GetActive()
}
return false, "", false, false
}