-
Notifications
You must be signed in to change notification settings - Fork 0
/
fill_region_mode.go
71 lines (65 loc) · 1.8 KB
/
fill_region_mode.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
package main
import (
"bytes"
"strconv"
)
type fill_region_context struct {
g *godit
prefix []byte
maxv int
}
// just a couple of prefixes for popular languages, sorted from long to short
var fill_region_prefixes = [][]byte{
[]byte(";;;;"), // Lisp
[]byte(";;;"), // Lisp
[]byte("REM"), // cmd.exe, COMMAND.COM, Basic
[]byte("///"), // doxygen
[]byte("//"), // C, C++, C#, D, Go, Java, JavaScript, Delphi, PHP, etc.
[]byte(";;"), // Lisp
[]byte("--"), // Haskell, Lua, Ada, SQL, etc.
[]byte("::"), // md.exe, COMMAND.COM, Basic
[]byte("#"), // Perl, Python, Ruby, Bash, PHP, etc.
[]byte(";"), // Lisp
[]byte(":"), // cmd.exe, COMMAND.COM, Basic
// TODO: more?
}
func (f *fill_region_context) maxv_lemp() line_edit_mode_params {
v := f.g.active.leaf
return line_edit_mode_params{
prompt: "Fill width:",
initial_content: "80",
on_apply: func(buf *buffer) {
if i, err := strconv.Atoi(string(buf.contents())); err == nil {
f.maxv = i
}
v.finalize_action_group()
v.last_vcommand = vcommand_none
v.fill_region(f.maxv, f.prefix)
v.finalize_action_group()
},
}
}
func (f *fill_region_context) prefix_lemp() line_edit_mode_params {
return line_edit_mode_params{
prompt: "Prefix:",
initial_content: string(f.prefix),
on_apply: func(buf *buffer) {
f.prefix = buf.contents()
f.g.set_overlay_mode(init_line_edit_mode(f.g, f.maxv_lemp()))
},
}
}
func init_fill_region_mode(godit *godit) *line_edit_mode {
v := godit.active.leaf
f := fill_region_context{g: godit, maxv: 80}
beg, _ := v.line_region()
data := beg.line.data
data = data[index_first_non_space(data):]
for _, prefix := range fill_region_prefixes {
if bytes.HasPrefix(data, prefix) {
f.prefix = prefix
break
}
}
return init_line_edit_mode(godit, f.prefix_lemp())
}