-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
863 lines (717 loc) · 18.1 KB
/
main.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
package main
import (
"bytes"
"flag"
"fmt"
"net/url"
"os"
"os/exec"
"path"
"regexp"
"runtime/debug"
"strconv"
"strings"
"github.com/jeffwilliams/basejump/diff"
"github.com/neovim/go-client/nvim"
"github.com/neovim/go-client/nvim/plugin"
)
type Basejump struct {
P *plugin.Plugin
}
func (n Basejump) nvim() *nvim.Nvim {
return n.P.Nvim
}
// Echom formats it's arguments using fmt.Sprintf, then performs the echom command with the resulting
// string. Basically a printf to vim's status line and stores it in vim's messages.
func (n Basejump) Echom(fmts string, args ...interface{}) {
s := fmt.Sprintf(fmts, args...)
s = strings.Replace(s, "'", "''", -1)
n.P.Nvim.Command(fmt.Sprintf(":echom '%s'", s))
}
// Selection returns the coordinates of the current selection, if it is within a single line
// Note that if we are not in visual mode, this returns the last selection in visual mode.
func (n Basejump) Selection() (startLine, startCol, endLine, endCol int, err error) {
result := make([]float32, 4)
nv := n.nvim()
err = nv.Call("getpos", result, "'<")
if err != nil {
return
}
startLine = int(result[1])
startCol = int(result[2])
err = nv.Call("getpos", result, "'>")
if err != nil {
return
}
endLine = int(result[1])
endCol = int(result[2])
return
}
// Return the current line and column of the cursor
func (n Basejump) Cursor() (line, col int, err error) {
result := make([]float32, 4)
nv := n.nvim()
err = nv.Call("getpos", result, ".")
if err != nil {
return
}
line = int(result[1])
col = int(result[2])
return
}
// SelectionText returns the text contained in the current selection.
func (n Basejump) SelectionText() (text string, err error) {
nv := n.nvim()
var startLine, startCol, endLine, endCol int
startLine, startCol, endLine, endCol, err = n.Selection()
if err != nil {
return
}
trace(n, "selected text is from line %d col %d to line %d col %d", startLine, startCol, endLine, endCol)
var buf nvim.Buffer
buf, err = nv.CurrentBuffer()
if err != nil {
return
}
var lines [][]byte
// Indexing is zero-based, end-exclusive
lines, err = nv.BufferLines(buf, startLine-1, endLine, true)
if err != nil {
return
}
var bbuf bytes.Buffer
if len(lines) == 1 {
bbuf.Write(lines[0][startCol-1 : endCol])
} else if len(lines) > 1 {
bbuf.Write(lines[0][startCol-1 : len(lines[0])])
for i := 1; i < len(lines)-1; i++ {
bbuf.Write(lines[i])
}
bbuf.Write(lines[len(lines)-1][0:endCol])
}
text = bbuf.String()
return
}
// CurrentWordText returns the current word under the cursor
func (n Basejump) CurrentWordText() (text string, err error) {
nv := n.nvim()
err = nv.Call("expand", &text, "<cWORD>")
return
}
func (n Basejump) CurrentLineNumber() (num int, err error) {
nv := n.nvim()
err = nv.Call("line", &num, ".")
return
}
func (n Basejump) CurrentLineText() (text string, err error) {
nv := n.nvim()
err = nv.Call("getline", &text, ".")
return
}
func (n Basejump) LineText(line int) (text string, err error) {
nv := n.nvim()
err = nv.Call("getline", &text, line)
return
}
var pathRegex = regexp.MustCompile(`^([^:]+)(?::(\d+))?(?::(\d+))?`)
// ParsePath parses `text` into a filesystem path, line, and column. The `text`
// parameter must have one of the formats:
//
// <path> (for example file.go, or /bin/bash)
// <path>:<line> (for example file.go:100)
// <path>:<line>:<col> (for example file.go:100:20)
//
// If the parsed path is not absolute it is made absolute by prepending the
// cwd of the current window in vim.
//
// If line and or col is missing, they are set to 0.
func (n Basejump) ParsePath(text string) (fpath string, line, col int, err error) {
text = strings.TrimSpace(text)
match := pathRegex.FindStringSubmatch(text)
if match == nil || len(match) < 2 {
err = fmt.Errorf("doesn't seem to be a valid path")
return
}
fpath = match[1]
if len(match) > 2 && match[2] != "" {
line, err = strconv.Atoi(match[2])
if err != nil {
return
}
}
if len(match) > 3 && match[3] != "" {
col, err = strconv.Atoi(match[3])
if err != nil {
return
}
}
fpath, err = n.AbsPath(fpath)
if err != nil {
return
}
return
}
// AbsPath makes the path `fpath` absolute if it is not by prepending
// the working directory of the current window.
func (n Basejump) AbsPath(fpath string) (result string, err error) {
return n.AbsPathRelWindow(fpath, -1)
}
func (n Basejump) pidCwd(pid int) (string, error) {
return os.Readlink(fmt.Sprintf("/proc/%d/cwd", pid))
}
// AbsPathRelWindow makes the path `fpath` absolute if it is not by prepending
// the working directory of the window `window`.
func (n Basejump) AbsPathRelWindow(fpath string, window int) (result string, err error) {
nv := n.nvim()
result = fpath
if !path.IsAbs(fpath) {
var cwd string
if window == -1 {
// If the current window is a terminal window, then we need
// to get the cwd in a special way. We can't use this method for
// the non-current window.
var b bool
err = nv.Call("exists", &b, "b:term_title")
if err != nil {
return
}
if b {
// Is a terminal.
var pid float32
var buf nvim.Buffer
buf, err = nv.CurrentBuffer()
if err != nil {
return
}
err = nv.BufferVar(buf, "terminal_job_pid", &pid)
if err != nil {
return
}
cwd, err = n.pidCwd(int(pid))
if err != nil {
return
}
}
}
if cwd == "" {
args := make([]interface{}, 0, 1)
if window != -1 {
args = append(args, window)
}
err = nv.Call("getcwd", &cwd, args...)
if err != nil {
return
}
}
result = cwd + "/" + fpath
}
return
}
type SearchType int
const (
SearchOnlyInCurrentTab = iota
SearchInAllTabs
)
func (n Basejump) findWindow(fpath string, srchType SearchType) (win *nvim.Window, tabNr, winNr int, err error) {
nv := n.nvim()
defer func() {
if win != nil {
trace(n, "trace: findWindow: `%s` found in tab %d window %d", fpath, tabNr, winNr)
} else {
trace(n, "trace: findWindow: `%s` not found in open windows", fpath)
}
}()
nfpath, err := n.AbsPath(fpath)
if err == nil {
fpath = nfpath
}
findInList := func(wins []nvim.Window) {
for _, cwin := range wins {
var buf nvim.Buffer
var bufFileName string
buf, err = nv.WindowBuffer(cwin)
if err != nil {
trace(n, "trace: findWindow: WindowBuffer(%d) failed: %v", cwin, err)
return
}
bufFileName, err = nv.BufferName(buf)
if err != nil {
trace(n, "trace: findWindow: BufferName failed: %v", err)
return
}
winNr, err = nv.WindowNumber(cwin)
if err != nil {
trace(n, "trace: findWindow: WindowNumber(%d) failed: %v", cwin, err)
return
}
bufFileName, err = n.AbsPathRelWindow(bufFileName, winNr)
if err != nil {
trace(n, "trace: findWindow: n.AbsPathRelWindow failed: %v", err)
return
}
trace(n, "trace: findWindow: '%s' vs '%s'", path.Clean(bufFileName), path.Clean(fpath))
if path.Clean(bufFileName) == path.Clean(fpath) {
win = &cwin
return
}
}
return
}
if srchType == SearchOnlyInCurrentTab {
trace(n, "trace: findWindow: searching for `%s` in current tab's windows", fpath)
var wins []nvim.Window
wins, err = nv.Windows()
if err != nil {
return
}
findInList(wins)
} else {
trace(n, "trace: findWindow: searching for `%s` in all tabs windows", fpath)
var tabs []nvim.Tabpage
tabs, err = nv.Tabpages()
if err != nil {
return
}
// Move the current tab to the first, so that if the window is in the
// current tabpage and another one, we prefer the local one
var curTab nvim.Tabpage
curTab, err = nv.CurrentTabpage()
if err != nil {
return
}
for i, tab := range tabs {
if tab == curTab && i > 0 {
t := tabs[0]
tabs[0] = tabs[i]
tabs[i] = t
break
}
}
for _, tab := range tabs {
var wins []nvim.Window
wins, err = nv.TabpageWindows(tab)
if err != nil {
return
}
tabNr, err = nv.TabpageNumber(tab)
if err != nil {
return
}
findInList(wins)
if err != nil || win != nil {
return
}
}
}
return
}
// OpenOrChangeTo ensures the specified file is open in vim. If the path is found in a
// window, that window is made current. If no window contains that path, it is split and
// opened.
func (n Basejump) OpenOrChangeTo(fpath, method string) (wasOpen bool, err error) {
nv := n.nvim()
//win, winNr, err := n.findWindow(fpath, SearchOnlyInCurrentTab)
win, tabNr, winNr, err := n.findWindow(fpath, SearchInAllTabs)
if err != nil {
return
}
if win != nil {
// Change to this window
trace(n, "trace: SplitOrChangeTo: changing to tab")
nv.Command(fmt.Sprintf("%dtabnext", tabNr))
trace(n, "trace: SplitOrChangeTo: changing to existing window")
err = nv.Command(fmt.Sprintf("%dwincmd w", winNr))
wasOpen = true
return
}
// Not found. Split new window
// Seems like :split is not working from a script for directories for me
// (see https://superuser.com/questions/1243344/vim-wont-split-open-a-directory-from-python-but-it-works-interactively)
// so if it's a directory, use Hexplore instead.
isDir := false
var fi os.FileInfo
if fi, err = os.Stat(fpath); err == nil && fi.IsDir() {
isDir = true
}
action := "splitting"
dirCmd := "Hexplore"
splitCmd := "split"
if method == openByTab {
action = "tabbing"
dirCmd = "Texplore"
splitCmd = "tabedit"
}
trace(n, "trace: SplitOrChangeTo: no window matches. %s %s.", action, fpath)
if isDir {
// If it's a directory, use :Hexplore instead.
err = nv.Command(fmt.Sprintf("%s %s", dirCmd, fpath))
} else {
err = nv.Command(fmt.Sprintf("%s %s", splitCmd, fpath))
}
return
}
func commandWhichExists(cmds []string) string {
for _, cmd := range cmds {
path, err := exec.LookPath(cmd)
if err == nil {
return path
}
}
return ""
}
func (n Basejump) OpenRemoteUrl(url *url.URL, method string) error {
nv := n.nvim()
browsers := []string{"elinks", "w3m", "links", "lynx"}
err := nv.Var("basejump_browsers", browsers)
if err != nil {
n.Echom("basejump_browsers is not defined (%v). Defaulting to %s", err, browsers)
}
b := commandWhichExists(browsers)
if b == "" {
return fmt.Errorf("error: no browser found. Tried %v", browsers)
}
action := "new"
if method == openByTab {
action = "tabnew"
}
err = nv.Command(action)
if err != nil {
return err
}
err = nv.Call("termopen", nil, fmt.Sprintf("%s %s", b, url))
return err
}
func (n Basejump) OpenPath(text, method string) error {
var path string
var line, col int
nv := n.nvim()
trace(n, "trace: checking for URL")
// First, check for a URL
url, err := url.Parse(text)
if err == nil {
if url.Scheme == "file" {
path = url.Path
} else if url.Scheme == "http" || url.Scheme == "https" {
// Handle a remote URL specially
return n.OpenRemoteUrl(url, method)
}
}
if path == "" {
trace(n, "trace: parsing path")
path, line, col, err = n.ParsePath(text)
if err != nil {
return err
}
}
var openNonexistent int
nv.Var("basejump_open_nonexistent", &openNonexistent)
trace(n, "trace: checking if path exists")
if openNonexistent == 0 && !pathExists(path) {
return fmt.Errorf("error: no such file '%s'", path)
}
/*
trace(n, "trace: ensuring file is open or opening it")
var wasOpen bool
wasOpen, err = n.OpenOrChangeTo(path, method)
if err != nil {
return err
}
if col == 0 {
col = 1
}
if !wasOpen {
if line == 0 {
line = 1
}
err = n.JumpToLineAndCol(line, col)
} else {
if line != 0 {
err = n.JumpToLineAndCol(line, col)
}
}
if err != nil {
return err
}
*/
n.OpenPathAtLineCol(path, line, col, method)
return nil
}
func (n Basejump) OpenPathAtLineCol(path string, line, col int, method string) (err error) {
trace(n, "trace: ensuring file is open or opening it")
var wasOpen bool
wasOpen, err = n.OpenOrChangeTo(path, method)
if err != nil {
return
}
if col == 0 {
col = 1
}
if !wasOpen {
if line == 0 {
line = 1
}
err = n.JumpToLineAndCol(line, col)
} else {
if line != 0 {
err = n.JumpToLineAndCol(line, col)
}
}
return
}
func (n Basejump) OpenSelectedPath(method string) error {
trace(n, "trace: obtaining selected text")
text, err := n.SelectionText()
if err != nil {
return err
}
if len(text) == 0 {
err = fmt.Errorf("selection is empty")
return err
}
// To expand tildes into home directories, we need a second expand
nv := n.nvim()
err = nv.Call("expand", &text, text)
if err != nil {
return err
}
return n.OpenPath(text, method)
}
func (n Basejump) OpenPathUnderCursor(method string) error {
text, err := n.CurrentLineText()
if err != nil {
return err
}
_, col, err := n.Cursor()
if err != nil {
return err
}
nv := n.nvim()
var pathChars string
err = nv.Var("basejump_pathchars", &pathChars)
if err != nil {
pathChars = "-~/[a-z][A-Z].:[0-9]"
n.Echom("basejump_pathchars is not defined. Defaulting to %s", pathChars)
}
text = matching(text, col-1, pathChars)
// To expand tildes into home directories, we need a second expand
err = nv.Call("expand", &text, text)
if err != nil {
return err
}
return n.OpenPath(text, method)
}
// JumpToLineAndCol moves the cursor to the specified line and column in the
// current buffer.
func (n Basejump) JumpToLineAndCol(line, col int) (err error) {
nv := n.P.Nvim
// In order to store these jumps in the jump history
// we use the 'G' command first. This only stores the line
// number, though, with column 1 (instead of the correct column).
// We store a second jump by doing a forward search for any character,
// with the count of the column number i.e. 10/.
nv.Command(fmt.Sprintf("normal %dG", line))
if col > 1 {
nv.Command(fmt.Sprintf("normal %d/.", col-1))
}
// Just to make sure we didn't mess up
err = nv.Call("cursor", nil, line, col)
return
}
func (n Basejump) OpenLineFromDiff(method string) error {
// When the diff code checks if a path exists, we want it to be
// relative to the current window, not to the basejump process.
pe := func(path string) bool {
npath, err := n.AbsPath(path)
if err != nil {
trace(n, "trace: OpenLineFromDiff: AbsPath(%s) failed: %v", path, err)
npath = path
}
trace(n, "trace: OpenLineFromDiff: npath is %s", npath)
return pathExists(npath)
}
path, lineNo, err := diff.CalcFileAndLine(n, pe)
trace(n, "trace: diff file: computed file '%s' line %d", path, lineNo)
if err != nil {
return err
}
return n.OpenPathAtLineCol(path, lineNo, 1, method)
}
func expandCharRanges(chars string) string {
crunes := []rune(chars)
output := make([]rune, 0, 26)
const (
inside = iota
outside
)
state := outside
start := -1
var low, high rune
var i int
// false alarm. Was not a range
restart := func() {
i = start
output = append(output, '[')
state = outside
}
expand := func() {
if high < low {
low, high = high, low
}
for r := low; r <= high; r++ {
output = append(output, r)
}
}
for i = 0; i < len(crunes); i++ {
r := crunes[i]
//fmt.Println("i=", i, "r=", string(r))
if state == inside {
// handle each position within [X-Y]
switch i - start {
case 1:
low = r
case 2:
if r != '-' {
restart()
continue
}
case 3:
high = r
case 4:
if r != ']' {
restart()
continue
}
state = outside
expand()
}
if i == len(crunes)-1 && state != outside {
restart()
}
} else {
switch r {
case '[':
if i < len(crunes)-1 {
state = inside
start = i
} else {
output = append(output, r)
}
default:
output = append(output, r)
}
}
}
return string(output)
}
// Starting at `index` in string `s`, move forwards and backwards
// to find the longest string around `index` that contains only characters in
// `chars`.
func matching(s string, index int, chars string) string {
if index < 0 || index >= len(s) {
return ""
}
srunes := []rune(s)
crunes := []rune(expandCharRanges(chars))
good := func(i int) bool {
for _, r := range crunes {
if srunes[i] == r {
return true
}
}
return false
}
if !good(index) {
return ""
}
left := index
for ; left >= 0; left-- {
if !good(left) {
break
}
}
right := index
for ; right < len(s); right++ {
if !good(right) {
break
}
}
return string(srunes[left+1 : right])
}
func pathExists(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}
var optLogPanic = flag.Bool("logpanic", false, "log panics to the file /tmp/basejump.panic")
var optTrace = flag.Bool("trace", false, "trace execution to messages history")
func logPanic() {
if v := recover(); v != nil {
f, err := os.Create("/tmp/basejump.panic")
if err != nil {
return
}
fmt.Fprintf(f, "%s\n", v)
fmt.Fprintf(f, "%s\n", debug.Stack())
f.Close()
}
}
func trace(n Basejump, fmt string, args ...interface{}) {
if *optTrace {
n.Echom(fmt, args...)
}
}
const (
openBySplit = "split"
openByTab = "tab"
)
func main() {
flag.Parse()
plugin.Main(func(p *plugin.Plugin) error {
a := Basejump{p}
openSelectedPath := func(args []string) (string, error) {
if *optLogPanic {
defer logPanic()
}
if len(args) == 0 {
args = append(args, openBySplit)
}
err := a.OpenSelectedPath(args[0])
if err != nil {
a.Echom("error: %v", err)
}
// Returning an error here prints too much overdramatic red text
return "", nil
}
openPathUnderCursor := func(args []string) (string, error) {
if *optLogPanic {
defer logPanic()
}
if len(args) == 0 {
args = append(args, openBySplit)
}
err := a.OpenPathUnderCursor(args[0])
if err != nil {
a.Echom("error: %v", err)
}
// Returning an error here prints too much overdramatic red text
return "", nil
}
openLineFromDiff := func(args []string) (string, error) {
if *optLogPanic {
defer logPanic()
}
if len(args) == 0 {
args = append(args, openBySplit)
}
err := a.OpenLineFromDiff(args[0])
if err != nil {
a.Echom("error: %v", err)
}
// Returning an error here prints too much overdramatic red text
return "", nil
}
p.HandleFunction(&plugin.FunctionOptions{Name: "OpenSelectedPath"}, openSelectedPath)
p.HandleFunction(&plugin.FunctionOptions{Name: "OpenPathUnderCursor"}, openPathUnderCursor)
p.HandleFunction(&plugin.FunctionOptions{Name: "OpenLineFromDiff"}, openLineFromDiff)
return nil
})
}