2017-03-14 01:52:44 +00:00
|
|
|
package gonvim
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2017-05-12 04:07:54 +01:00
|
|
|
"runtime"
|
2017-03-14 07:20:31 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2017-06-02 10:41:28 +01:00
|
|
|
"sync"
|
2017-03-14 01:52:44 +00:00
|
|
|
"unsafe"
|
|
|
|
|
2017-03-16 10:38:16 +00:00
|
|
|
"github.com/dzhou121/neovim-fzf-shim/rplugin/go/fzf"
|
2017-04-28 11:25:13 +01:00
|
|
|
"github.com/dzhou121/neovim-locpopup/rplugin/go/locpopup"
|
2017-03-14 01:52:44 +00:00
|
|
|
"github.com/dzhou121/ui"
|
|
|
|
"github.com/neovim/go-client/nvim"
|
|
|
|
)
|
|
|
|
|
|
|
|
var editor *Editor
|
|
|
|
|
|
|
|
// Highlight is
|
|
|
|
type Highlight struct {
|
|
|
|
foreground *RGBA
|
|
|
|
background *RGBA
|
|
|
|
}
|
|
|
|
|
|
|
|
// Char is
|
|
|
|
type Char struct {
|
|
|
|
char string
|
|
|
|
highlight Highlight
|
|
|
|
}
|
|
|
|
|
|
|
|
// Editor is the editor
|
|
|
|
type Editor struct {
|
2017-05-10 07:28:44 +01:00
|
|
|
nvim *nvim.Nvim
|
|
|
|
nvimAttached bool
|
|
|
|
mode string
|
|
|
|
font *Font
|
|
|
|
smallerFont *Font
|
|
|
|
rows int
|
|
|
|
cols int
|
|
|
|
cursor *CursorBox
|
2017-06-02 10:53:27 +01:00
|
|
|
Foreground *RGBA
|
|
|
|
Background *RGBA
|
2017-06-02 10:55:33 +01:00
|
|
|
special *RGBA
|
2017-05-10 07:28:44 +01:00
|
|
|
window *ui.Window
|
|
|
|
screen *Screen
|
|
|
|
areaBox *ui.Box
|
|
|
|
close chan bool
|
|
|
|
popup *PopupMenu
|
|
|
|
finder *Finder
|
|
|
|
tabline *Tabline
|
|
|
|
statusline *Statusline
|
|
|
|
statuslineHeight int
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
tablineHeight int
|
|
|
|
selectedBg *RGBA
|
|
|
|
matchFg *RGBA
|
2017-06-02 10:41:28 +01:00
|
|
|
resizeMutex sync.Mutex
|
|
|
|
redrawMutex sync.Mutex
|
2017-03-14 01:52:44 +00:00
|
|
|
}
|
|
|
|
|
2017-05-05 09:37:20 +01:00
|
|
|
func initMainWindow(box *ui.Box, width, height int) *ui.Window {
|
2017-03-14 01:52:44 +00:00
|
|
|
window := ui.NewWindow("Gonvim", width, height, false)
|
|
|
|
window.SetChild(box)
|
|
|
|
window.OnClosing(func(*ui.Window) bool {
|
|
|
|
ui.Quit()
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
window.OnContentSizeChanged(func(w *ui.Window, data unsafe.Pointer) bool {
|
2017-03-22 21:12:12 +00:00
|
|
|
if editor == nil {
|
2017-03-22 21:13:22 +00:00
|
|
|
return true
|
2017-03-22 21:12:12 +00:00
|
|
|
}
|
2017-03-14 01:52:44 +00:00
|
|
|
width, height = window.ContentSize()
|
2017-06-02 10:41:28 +01:00
|
|
|
fmt.Println("window changed", width, height)
|
2017-03-22 22:13:36 +00:00
|
|
|
if width == editor.width && height == editor.height {
|
2017-03-22 22:14:43 +00:00
|
|
|
return true
|
2017-03-22 22:13:36 +00:00
|
|
|
}
|
2017-06-02 10:41:28 +01:00
|
|
|
editor.resize(width, height)
|
2017-03-14 01:52:44 +00:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
window.Show()
|
|
|
|
return window
|
|
|
|
}
|
|
|
|
|
2017-06-02 10:41:28 +01:00
|
|
|
func getTablineHeight(font *Font) int {
|
|
|
|
return font.lineHeight + 12
|
|
|
|
}
|
|
|
|
|
|
|
|
func getStatuslineHeight(font *Font) int {
|
|
|
|
return font.lineHeight + 6
|
|
|
|
}
|
|
|
|
|
2017-03-14 01:52:44 +00:00
|
|
|
// InitEditor inits the editor
|
|
|
|
func InitEditor() error {
|
|
|
|
if editor != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2017-06-02 10:41:28 +01:00
|
|
|
|
|
|
|
fontFamily := ""
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "windows":
|
|
|
|
fontFamily = "Consolas"
|
|
|
|
case "darwin":
|
|
|
|
fontFamily = "Courier New"
|
|
|
|
default:
|
|
|
|
fontFamily = "Monospace"
|
|
|
|
}
|
|
|
|
font := initFont(fontFamily, 14, 6)
|
|
|
|
smallerFont := initFont(fontFamily, 12, 0)
|
|
|
|
|
2017-03-14 01:52:44 +00:00
|
|
|
width := 800
|
|
|
|
height := 600
|
2017-06-02 10:41:28 +01:00
|
|
|
tablineHeight := getTablineHeight(font)
|
|
|
|
statuslineHeight := getStatuslineHeight(font)
|
|
|
|
screenHeight := height - tablineHeight - statuslineHeight
|
2017-03-21 18:17:06 +00:00
|
|
|
|
2017-06-02 10:41:28 +01:00
|
|
|
screen := initScreen(width, screenHeight)
|
|
|
|
cursor := initCursorBox(width, screenHeight)
|
2017-03-14 01:52:44 +00:00
|
|
|
popupMenu := initPopupmenu()
|
2017-03-16 10:38:16 +00:00
|
|
|
finder := initFinder()
|
2017-04-27 11:32:04 +01:00
|
|
|
tabline := initTabline(width, tablineHeight)
|
2017-04-28 11:25:13 +01:00
|
|
|
loc := initLocpopup()
|
2017-05-10 07:28:44 +01:00
|
|
|
statusline := initStatusline(width, statuslineHeight)
|
2017-03-14 01:52:44 +00:00
|
|
|
|
|
|
|
box := ui.NewHorizontalBox()
|
2017-04-28 02:50:38 +01:00
|
|
|
areaBox := ui.NewHorizontalBox()
|
2017-05-05 09:37:20 +01:00
|
|
|
areaBox.Append(screen.box, false)
|
2017-04-28 11:25:13 +01:00
|
|
|
areaBox.Append(cursor.box, false)
|
|
|
|
areaBox.Append(loc.box, false)
|
2017-04-28 02:50:38 +01:00
|
|
|
areaBox.Append(popupMenu.box, false)
|
|
|
|
areaBox.Append(finder.box, false)
|
2017-04-27 11:32:04 +01:00
|
|
|
box.Append(tabline.box, false)
|
2017-04-28 02:50:38 +01:00
|
|
|
box.Append(areaBox, false)
|
2017-05-10 07:28:44 +01:00
|
|
|
box.Append(statusline.box, false)
|
2017-03-14 01:52:44 +00:00
|
|
|
|
2017-06-02 10:41:28 +01:00
|
|
|
areaBox.SetSize(width, screenHeight)
|
2017-04-28 02:50:38 +01:00
|
|
|
areaBox.SetPosition(0, tablineHeight)
|
2017-06-02 10:41:28 +01:00
|
|
|
statusline.box.SetPosition(0, tablineHeight+screenHeight)
|
|
|
|
window := initMainWindow(box, width, height)
|
2017-03-14 01:52:44 +00:00
|
|
|
|
|
|
|
neovim, err := nvim.NewEmbedded(&nvim.EmbedOptions{
|
|
|
|
Args: os.Args[1:],
|
|
|
|
})
|
|
|
|
if err != nil {
|
2017-05-31 11:35:03 +01:00
|
|
|
fmt.Println("nvim start error", err)
|
|
|
|
ui.Quit()
|
2017-03-14 01:52:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
editor = &Editor{
|
2017-05-10 07:28:44 +01:00
|
|
|
nvim: neovim,
|
|
|
|
nvimAttached: false,
|
|
|
|
window: window,
|
|
|
|
screen: screen,
|
|
|
|
areaBox: areaBox,
|
|
|
|
mode: "normal",
|
|
|
|
close: make(chan bool),
|
|
|
|
cursor: cursor,
|
|
|
|
popup: popupMenu,
|
|
|
|
finder: finder,
|
|
|
|
tabline: tabline,
|
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
tablineHeight: tablineHeight,
|
|
|
|
statusline: statusline,
|
|
|
|
statuslineHeight: statuslineHeight,
|
|
|
|
font: font,
|
|
|
|
smallerFont: smallerFont,
|
|
|
|
selectedBg: newRGBA(81, 154, 186, 0.5),
|
|
|
|
matchFg: newRGBA(81, 154, 186, 1),
|
2017-03-14 01:52:44 +00:00
|
|
|
}
|
|
|
|
|
2017-06-02 10:41:28 +01:00
|
|
|
editor.nvimResize()
|
2017-03-14 07:20:31 +00:00
|
|
|
editor.handleNotification()
|
2017-03-16 10:38:16 +00:00
|
|
|
editor.finder.rePosition()
|
2017-03-14 01:52:44 +00:00
|
|
|
go func() {
|
2017-05-08 07:51:30 +01:00
|
|
|
err := neovim.Serve()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2017-03-14 01:52:44 +00:00
|
|
|
editor.close <- true
|
|
|
|
}()
|
|
|
|
|
|
|
|
o := make(map[string]interface{})
|
|
|
|
o["rgb"] = true
|
2017-04-27 11:32:04 +01:00
|
|
|
o["ext_popupmenu"] = true
|
|
|
|
o["ext_tabline"] = true
|
2017-05-25 07:17:01 +01:00
|
|
|
err = editor.nvim.AttachUI(editor.cols, editor.rows, o)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("nvim attach UI error", err)
|
|
|
|
ui.Quit()
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-14 07:26:11 +00:00
|
|
|
editor.nvim.Subscribe("Gui")
|
2017-03-14 07:20:31 +00:00
|
|
|
editor.nvim.Command("runtime plugin/nvim_gui_shim.vim")
|
|
|
|
editor.nvim.Command("runtime! ginit.vim")
|
2017-05-31 03:15:55 +01:00
|
|
|
editor.nvim.Command("let g:gonvim_running=1")
|
2017-03-16 10:38:16 +00:00
|
|
|
fzf.RegisterPlugin(editor.nvim)
|
2017-04-28 11:25:13 +01:00
|
|
|
locpopup.RegisterPlugin(editor.nvim)
|
2017-03-14 01:52:44 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-editor.close
|
|
|
|
ui.Quit()
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-14 07:20:31 +00:00
|
|
|
func (e *Editor) handleNotification() {
|
|
|
|
e.nvim.RegisterHandler("Gui", func(updates ...interface{}) {
|
2017-05-11 06:18:54 +01:00
|
|
|
go e.handleRPCGui(updates...)
|
2017-03-14 07:20:31 +00:00
|
|
|
})
|
2017-03-14 01:52:44 +00:00
|
|
|
e.nvim.RegisterHandler("redraw", func(updates ...[]interface{}) {
|
2017-06-02 10:41:28 +01:00
|
|
|
e.redrawMutex.Lock()
|
2017-05-11 08:48:09 +01:00
|
|
|
e.handleRedraw(updates...)
|
2017-06-02 10:41:28 +01:00
|
|
|
e.redrawMutex.Unlock()
|
2017-05-11 06:18:54 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Editor) handleRPCGui(updates ...interface{}) {
|
|
|
|
event := updates[0].(string)
|
|
|
|
switch event {
|
|
|
|
case "Font":
|
|
|
|
e.guiFont(updates[1:])
|
|
|
|
case "Linespace":
|
|
|
|
e.guiLinespace(updates[1:])
|
|
|
|
case "finder_pattern":
|
|
|
|
e.finder.showPattern(updates[1:])
|
|
|
|
case "finder_pattern_pos":
|
|
|
|
e.finder.cursorPos(updates[1:])
|
|
|
|
case "finder_show_result":
|
|
|
|
e.finder.showResult(updates[1:])
|
|
|
|
case "finder_show":
|
|
|
|
e.finder.show()
|
|
|
|
case "finder_hide":
|
|
|
|
e.finder.hide()
|
|
|
|
case "finder_select":
|
|
|
|
e.finder.selectResult(updates[1:])
|
|
|
|
case "locpopup_show":
|
|
|
|
arg, ok := updates[1].(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return
|
2017-03-14 01:52:44 +00:00
|
|
|
}
|
2017-05-11 06:18:54 +01:00
|
|
|
e.cursor.locpopup.show(arg)
|
|
|
|
case "locpopup_hide":
|
|
|
|
e.cursor.locpopup.hide()
|
|
|
|
case "signature_show":
|
|
|
|
e.cursor.signature.show(updates[1:])
|
|
|
|
case "signature_pos":
|
|
|
|
e.cursor.signature.pos(updates[1:])
|
|
|
|
case "signature_hide":
|
|
|
|
e.cursor.signature.hide()
|
|
|
|
default:
|
|
|
|
fmt.Println("unhandled Gui event", event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Editor) handleRedraw(updates ...[]interface{}) {
|
|
|
|
screen := e.screen
|
2017-05-16 07:16:53 +01:00
|
|
|
go screen.redrawWindows()
|
2017-05-11 06:18:54 +01:00
|
|
|
for _, update := range updates {
|
|
|
|
event := update[0].(string)
|
|
|
|
args := update[1:]
|
|
|
|
switch event {
|
|
|
|
case "update_fg":
|
|
|
|
args := update[1].([]interface{})
|
2017-05-25 06:17:31 +01:00
|
|
|
color := reflectToInt(args[0])
|
|
|
|
if color == -1 {
|
2017-06-02 10:53:27 +01:00
|
|
|
editor.Foreground = newRGBA(255, 255, 255, 1)
|
2017-05-25 06:17:31 +01:00
|
|
|
} else {
|
|
|
|
editor.Foreground = calcColor(reflectToInt(args[0]))
|
|
|
|
}
|
2017-05-11 06:18:54 +01:00
|
|
|
case "update_bg":
|
|
|
|
args := update[1].([]interface{})
|
2017-05-25 06:17:31 +01:00
|
|
|
color := reflectToInt(args[0])
|
|
|
|
if color == -1 {
|
2017-06-02 10:53:27 +01:00
|
|
|
editor.Background = newRGBA(0, 0, 0, 1)
|
2017-05-25 06:17:31 +01:00
|
|
|
} else {
|
|
|
|
bg := calcColor(reflectToInt(args[0]))
|
|
|
|
editor.Background = bg
|
|
|
|
}
|
2017-06-02 10:55:33 +01:00
|
|
|
case "update_sp":
|
|
|
|
args := update[1].([]interface{})
|
|
|
|
color := reflectToInt(args[0])
|
|
|
|
if color == -1 {
|
|
|
|
editor.special = newRGBA(255, 255, 255, 1)
|
|
|
|
} else {
|
|
|
|
editor.special = calcColor(reflectToInt(args[0]))
|
|
|
|
}
|
2017-05-11 06:18:54 +01:00
|
|
|
case "cursor_goto":
|
|
|
|
screen.cursorGoto(args)
|
|
|
|
case "put":
|
|
|
|
screen.put(args)
|
|
|
|
case "eol_clear":
|
|
|
|
screen.eolClear(args)
|
|
|
|
case "clear":
|
|
|
|
screen.clear(args)
|
|
|
|
case "resize":
|
|
|
|
screen.resize(args)
|
|
|
|
case "highlight_set":
|
|
|
|
screen.highlightSet(args)
|
|
|
|
case "set_scroll_region":
|
|
|
|
screen.setScrollRegion(args)
|
|
|
|
case "scroll":
|
|
|
|
screen.scroll(args)
|
|
|
|
case "mode_change":
|
2017-05-16 07:16:53 +01:00
|
|
|
arg := update[len(update)-1].([]interface{})
|
2017-05-11 06:18:54 +01:00
|
|
|
editor.mode = arg[0].(string)
|
|
|
|
case "popupmenu_show":
|
|
|
|
editor.popup.show(args)
|
|
|
|
case "popupmenu_hide":
|
|
|
|
editor.popup.hide(args)
|
|
|
|
case "popupmenu_select":
|
|
|
|
editor.popup.selectItem(args)
|
|
|
|
case "tabline_update":
|
|
|
|
editor.tabline.update(args)
|
2017-06-02 10:55:33 +01:00
|
|
|
case "busy_start":
|
|
|
|
case "busy_stop":
|
2017-05-11 06:18:54 +01:00
|
|
|
default:
|
|
|
|
fmt.Println("Unhandle event", event)
|
2017-03-14 01:52:44 +00:00
|
|
|
}
|
2017-05-11 06:18:54 +01:00
|
|
|
}
|
|
|
|
screen.redraw()
|
2017-05-11 08:48:09 +01:00
|
|
|
editor.cursor.draw()
|
2017-05-11 06:18:54 +01:00
|
|
|
if !e.nvimAttached {
|
|
|
|
e.nvimAttached = true
|
|
|
|
}
|
2017-06-02 10:41:28 +01:00
|
|
|
go editor.statusline.redraw(false)
|
2017-03-14 01:52:44 +00:00
|
|
|
}
|
2017-03-14 07:20:31 +00:00
|
|
|
|
|
|
|
func (e *Editor) guiFont(args ...interface{}) {
|
|
|
|
fontArg := args[0].([]interface{})
|
|
|
|
parts := strings.Split(fontArg[0].(string), ":")
|
|
|
|
if len(parts) < 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
height := 14
|
|
|
|
for _, p := range parts[1:] {
|
|
|
|
if strings.HasPrefix(p, "h") {
|
|
|
|
var err error
|
|
|
|
height, err = strconv.Atoi(p[1:])
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
e.font.change(parts[0], height)
|
2017-05-02 07:55:26 +01:00
|
|
|
e.smallerFont.change(parts[0], height-2)
|
2017-06-02 10:41:28 +01:00
|
|
|
e.resize(e.width, e.height)
|
2017-03-14 07:20:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Editor) guiLinespace(args ...interface{}) {
|
|
|
|
fontArg := args[0].([]interface{})
|
2017-06-02 13:29:05 -07:00
|
|
|
var lineSpace int
|
|
|
|
var err error
|
|
|
|
switch arg := fontArg[0].(type) {
|
|
|
|
case string:
|
|
|
|
lineSpace, err = strconv.Atoi(arg)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case int32: // can't combine these in a type switch without compile error
|
|
|
|
lineSpace = int(arg)
|
|
|
|
case int64:
|
|
|
|
lineSpace = int(arg)
|
|
|
|
default:
|
2017-03-14 07:20:31 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
e.font.changeLineSpace(lineSpace)
|
2017-05-02 07:55:26 +01:00
|
|
|
e.smallerFont.changeLineSpace(lineSpace)
|
2017-06-02 10:41:28 +01:00
|
|
|
e.resize(e.width, e.height)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Editor) resize(width, height int) {
|
|
|
|
ui.QueueMain(func() {
|
|
|
|
e.resizeMutex.Lock()
|
|
|
|
defer e.resizeMutex.Unlock()
|
|
|
|
e.tablineHeight = getTablineHeight(e.font)
|
|
|
|
e.statuslineHeight = getStatuslineHeight(e.font)
|
|
|
|
screenHeight := height - e.tablineHeight - e.statuslineHeight
|
|
|
|
e.width = width
|
|
|
|
e.height = height
|
|
|
|
e.areaBox.SetSize(width, screenHeight)
|
|
|
|
e.areaBox.SetPosition(0, e.tablineHeight)
|
|
|
|
e.screen.box.SetSize(width, screenHeight)
|
|
|
|
e.screen.setSize(width, screenHeight)
|
|
|
|
e.cursor.setSize(width, screenHeight)
|
|
|
|
e.tabline.resize(width, e.tablineHeight)
|
|
|
|
e.statusline.redraw(true)
|
|
|
|
e.statusline.box.SetSize(width, e.statuslineHeight)
|
|
|
|
e.statusline.setSize(width, e.statuslineHeight)
|
|
|
|
e.statusline.box.SetPosition(0, e.tablineHeight+screenHeight)
|
|
|
|
e.finder.rePosition()
|
|
|
|
e.nvimResize()
|
|
|
|
})
|
2017-03-14 07:20:31 +00:00
|
|
|
}
|
|
|
|
|
2017-06-02 10:41:28 +01:00
|
|
|
func (e *Editor) nvimResize() {
|
|
|
|
e.redrawMutex.Lock()
|
|
|
|
defer e.redrawMutex.Unlock()
|
|
|
|
width := e.screen.width
|
|
|
|
height := e.screen.height
|
2017-05-12 04:55:21 +01:00
|
|
|
// cols := width / editor.font.width
|
|
|
|
cols := int(float64(width) / editor.font.truewidth)
|
2017-03-14 07:20:31 +00:00
|
|
|
rows := height / editor.font.lineHeight
|
|
|
|
oldCols := editor.cols
|
|
|
|
oldRows := editor.rows
|
2017-03-14 07:26:11 +00:00
|
|
|
editor.cols = cols
|
|
|
|
editor.rows = rows
|
|
|
|
if oldCols > 0 && oldRows > 0 {
|
2017-05-31 08:57:12 +01:00
|
|
|
if cols != oldCols || rows != oldRows {
|
|
|
|
editor.nvim.TryResizeUI(cols, rows)
|
|
|
|
}
|
2017-03-14 07:20:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-14 01:52:44 +00:00
|
|
|
func (hl *Highlight) copy() Highlight {
|
|
|
|
highlight := Highlight{}
|
|
|
|
if hl.foreground != nil {
|
|
|
|
highlight.foreground = hl.foreground.copy()
|
|
|
|
}
|
|
|
|
if hl.background != nil {
|
|
|
|
highlight.background = hl.background.copy()
|
|
|
|
}
|
|
|
|
return highlight
|
|
|
|
}
|